HTML:
<Asp: datalist id = "dlsubject" runat = "server">
<Itemtemplate>
Major <asp: dropdownlist id = "ddlmajor" runat = "server" datasource = "<% bindmajor () %> "datatextfield =" majorname "datavaluefield =" majorid "autopostback =" true "onselectedindexchanged =" ddlmajor_selectedindexchanged ">
</ASP: dropdownlist>
Direction <asp: dropdownlist id = "ddldirection" runat = "server" datatextfield = "directionname" datavaluefield = "directionid">
</ASP: dropdownlist>
</Itemtemplate>
</ASP: datalist>
C #: Public dataset bindmajor ()
{
Return new majorbll (). selectthemajorbydeptid (session ["deptid"]. tostring (); // obtain all majors of the department based on the department ID
}
// Both the professional and professional directions are placed in the template's dropdownlist control. Switch the datalist control to the template state and double-click the professional dropdownlist control to generate the selectedindexchanged event, you can see it in its HTML code.
Protected void ddlmajor_selectedindexchanged (Object sender, eventargs E)
{
Dropdownlist ddlmajor = (dropdownlist) sender; // The most important sentence
Dropdownlist ddldirection = (dropdownlist) ddlmajor. Parent. findcontrol ("ddldirection ");
Ddldirection. datasource = binddirection (ddlmajor. selectedvalue );
// Ddldirection. datatextfield = "directionname"; // it has already been set in HTML and does not need to be set again
// Ddldirection. datavaluefield = "directionid ";
Ddldirection. databind ();
} Public dataset binddirection (string p_strmajor)
{
Return new direblbll (). selectdiredirebymajor (p_strmajor); // obtain all directions for this major based on the professional ID
}
I think that using the method mentioned in the previous article to add an event handler to a control in the datalist control can also achieve the cascade of dropdownlist, but there is no experiment.
You need to implement data binding initialization for two dropdownlists in the itemdatabound event of the datalist control.
Cascade in the gridview control is similar. During the gridview control, two dropdownlist data bindings are initialized in the rowdatabound event.