Detailed implementation process of Asp.net dropdownlist without refreshing Ajax second-level linkage

Source: Internet
Author: User

Database implementation: Add two tables, table 1, pingpai, table 2, and type. The specific database implementation depends on your understanding:

Page code:

<Asp: scriptmanager id = "scriptmanager1" runat = "server">
</ASP: scriptmanager>
<Asp: updatepanel runat = "server">
<Contenttemplate>
<Asp: dropdownlist id = "dropdownlist1" runat = "server" onselectedindexchanged = "dropdownlist1_selectedindexchanged"
Width = "200">
</ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist2" runat = "server" width = "200">
</ASP: dropdownlist>
</Contenttemplate>
</ASP: updatepanel>

// The above code implements the refreshing effect of AJAX

Main program code:

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Binddrop ();
}
}
Private void binddrop ()
{
// Bind data to the drop-down list
String sqlstr = "select * From pingpai ";
Datatable dt = database. gettable (sqlstr );
Dropdownlist1.datatextfield = "pingpai"; // set the words displayed in the list
Dropdownlist1.datavaluefield = "typeid"; // set the fields obtained after the list is submitted. You can hide the bound data.
Dropdownlist1.datasource = DT. defaultview;
Dropdownlist1.databind ();
Dropdownlist1.items. insert (0, new listitem ("select the car brand", ""); // add content to the first item, focusing on adding after binding
Dropdownlist2.items. insert (0, new listitem ("select the car brand model", ""); // add content to the first item, focusing on adding after binding
}

Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs E)
{
Int typeid = convert. toint32 (dropdownlist1.selectedvalue); // After the page is loaded, dropdownlist1.datavaluefield hides the bound data and then queries the data to be displayed in dropdownlist2 based on it.
String sqlstr = "select * from type where typeid = '" + typeid + "'";

Datatable dt = database. gettable (sqlstr );
Dropdownlist2.datatextfield = "type"; // set the data displayed in the dropdownlist1 event selectedindexchanged dropdownlist2 list
Dropdownlist2.datasource = DT. defaultview;
Dropdownlist2.databind ();;
}

Related Article

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.