Cascade the nested dropdownlist in datalist

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.