c# - Dependent DropDownList in FormView -
i trying create 2 drop down lists in form view values displayed in second drop down dependent upon first drop down.the first list contains class numbers contained in sql table "classes" , second drop down list contains class sections in same sql table "classes". want able select class number , have sections correspond class number pop up.
example classes table:
number: section: sln: 210 1 a-1 210 2 a-2 210 3 a-3 340 1 b-1 340 7 b-7 i have first list number set using
<asp:dropdownlist id="ddlnumber" runat="server" datasourceid="sqldsclasses" autopostback="true" datatextfield="number" datavaluefield="number"> </asp:dropdownlist> drop down , <asp:sqldatasource id="sqldsclasses" runat="server" connectionstring="<%$ connectionstrings:reinstatementcs %>" selectcommand="select [prefix], [number], [location], [sln], [starttime], [endtime], [classday], [classcredit], [classsection] [classes]"> </asp:sqldatasource> corresponding sqldatasource so far have tried use
select [classsection] [classes] [number] = numberdropdownlist.datavaluefield section list: <asp:dropdownlist id="classsectiondropdownlist" runat="server" datasourceid="sqldsnumsec" datatextfield="classsection" datavaluefield="classsection" autopostback="true"> </asp:dropdownlist> the form view being used set follows:
<asp:formview id="fvstudentclass" runat="server" datasourceid="sqldsstudentclass" datasourceid2="sqldsaccess" emptydatatext="student class not completed"> <edititemtemplate> <table style="width:100%;"> <tr> <td> prefix: </td> <td> ucoll</td> <td> number: </td> <td> <asp:dropdownlist id="ddlnumber" runat="server" datasourceid="sqldsclasses" autopostback="true" datatextfield="number" datavaluefield="number"> </asp:dropdownlist> </td> <td> section: </td> <td> <asp:dropdownlist id="sectiondropdownlist" runat="server" datasourceid="sqldsnumsec" datatextfield="section" datavaluefield="section" autopostback="true"> </asp:dropdownlist> </td> </tr> </table> <asp:linkbutton id="updatebutton" runat="server" causesvalidation="true" commandname="update" text="update" /> <asp:linkbutton id="updatecancelbutton" runat="server" causesvalidation="false" commandname="cancel" text="cancel" /> </edititemtemplate> <insertitemtemplate> istransfer: <asp:checkbox id="istransfercheckbox" runat="server" checked='<%# bind("istransfer") %>' /> <br /> prefix: <asp:textbox id="prefixtextbox" runat="server" text='<%# bind("prefix") %>' /> <br /> number: <asp:textbox id="numbertextbox" runat="server" text='<%# bind("number") %>' /> <br /> section: <asp:textbox id="sectiontextbox" runat="server" text='<%# bind("section") %>' /> <br /> id: <asp:textbox id="idtextbox" runat="server" text='<%# bind("id") %>' /> <br /> <asp:linkbutton id="insertbutton" runat="server" causesvalidation="true" commandname="insert" text="insert" /> <asp:linkbutton id="insertcancelbutton" runat="server" causesvalidation="false" commandname="cancel" text="cancel" /> </insertitemtemplate> <itemtemplate> <table style="width:100%;"> <tr> <td> prefix:</td> <td> <asp:label id="prefixlabel" runat="server" text='<%# bind("prefix") %>' /> </td> <td> number:</td> <td> <asp:label id="numberlabel" runat="server" text='<%# bind("number") %>' /> </td> <td> classsection:</td> <td> <asp:label id="classsectionlabel" runat="server" text='<%# bind("classsection") %>' /> </td> </tr> <tr> <td> istransfer:</td> <td> <asp:checkbox id="istransfercheckbox" runat="server" checked='<%# bind("istransfer") %>' enabled="false" /> </td> </tr> </table> <asp:linkbutton id="editbutton" runat="server" causesvalidation="false" commandname="edit" text="edit" /> </itemtemplate> </asp:formview> with sqldatasource:
<asp:sqldatasource id="sqldsstudentclass" runat="server" connectionstring="<%$ connectionstrings:reinstatementcs %>" deletecommand="delete [studentclass] [sln] = @sln" insertcommand="insert [studentclass] ([sln],[id]) values (@sln, @id)" selectcommand="select distinct classes.number, classes.section, studentclass.id studentclass left join accesslist on accesslist.alid = studentclass.id join classes on studentclass.sln = classes.sln ([sln] = @sln)" <!--accesslist gives information user--> updatecommand="update [studentclass] set [sln] = @sln, [id] = @id [sln] = @sln"> <deleteparameters> <asp:parameter name="sln" type="int32" /> </deleteparameters> <insertparameters> <asp:parameter name="sln" type="string" /> <asp:parameter name="id" type="string" /> </insertparameters> <selectparameters> <asp:querystringparameter name="id" querystringfield="alid" type="string" /> </selectparameters> <updateparameters> <asp:parameter name="id" type="string" /> <asp:parameter name="sln" type="int32" /> </updateparameters> </asp:sqldatasource> example of form in action using example tables above:
number drop down: 210 -> select 210 340 section drop down: 1 2 -> select 3 3
take @ article:
http://csharpdotnetfreak.blogspot.com/2009/03/populate-dropdown-based-selection-other.html
it explains how achive want.
also, alternative, may use ajax: http://www.asp.net/ajaxlibrary/ajaxcontroltoolkitsamplesite/cascadingdropdown/cascadingdropdown.aspx
Comments
Post a Comment