Add onselectedindexchanged to the dropdownlist Control item in the gridview to implement cascading

Source: Internet
Author: User

The onselectedindexchanged event can be directly associated with the server. There is no need to associate a button. In the selectedindexchanged event, you can find the drop-down box event through the sender and find the current gridviewrow through the parent twice, find another row drop-down box in findcontrol and bind the data.
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" onrowdatabound = "gridview1_rowdatabound">
<Columns>
<Asp: templatefield>
<Headertemplate>
Serial number
</Headertemplate>
<Itemtemplate>
<Asp: Label id = "lblindex" runat = "server" text = "<% # container. dataitemindex %>"> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield>
<Headertemplate>
Contract signature
</Headertemplate>
<Itemtemplate>
<Asp: dropdownlist id = "ddlcontractno" runat = "server" autopostback = true onselectedindexchanged = "ddlcontractno_selectedindexchanged">
</ASP: dropdownlist>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield>
<Headertemplate>
Item No.
</Headertemplate>
<Itemtemplate>
<Asp: dropdownlist id = "ddlmaterielno" runat = "server">
</ASP: dropdownlist>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (this. Page. ispostback)
Return;
Binddata ();
}

Protected void binddata ()
{
Arraylist Al = new arraylist ();
For (INT I = 0; I <15; I ++)
{
Al. Add (I );
}
This. gridview1.datasource = Al;
This. gridview1.databind ();
}

/// <Summary>
/// Obtain all contracts
/// </Summary>
/// <Returns> </returns>
Protected list <contract> getallcontract ()
{
List <contract> List = new list <contract> ();
For (INT I = 0; I <5; I ++)
{
Contract Contract = new contract ();
Contract. ID = I + 1;
Contract. Name = "contract" + contract. ID;
Contract. No = "no" + contract. ID;
List. Add (contract );
}
Return list;
}

Protected list <materiel> getallmaterielbycontractno (string contractno)
{
List <materiel> List = new list <materiel> ();
For (INT I = 0; I <10; I ++)
{
Materiel materiel = new materiel ();
Materiel. ID = I + 1;
Materiel. Name = "materiel" + contractno + materiel. ID;
Materiel. No = contractno + materiel. ID;
Materiel. contractno = contractno;
List. Add (materiel );
}
Return list;
}

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype! = Datacontrolrowtype. datarow)
Return;
Dropdownlist ddlcontractno = (dropdownlist) E. Row. cells [1]. findcontrol ("ddlcontractno "));
Ddlcontractno. datasource = getallcontract ();
Ddlcontractno. datatextfield = "no ";
Ddlcontractno. databind ();
Ddlcontractno. selectedindex = 0;
Bindmaterieldropdownlist (E. Row, ddlcontractno. selectedvalue );
}

Protected void ddlcontractno_selectedindexchanged (Object sender, eventargs E)
{
Dropdownlist ddlcontractno = (dropdownlist) sender;
Gridviewrow ROW = (gridviewrow) ddlcontractno. Parent. parent;
Bindmaterieldropdownlist (row, ddlcontractno. selectedvalue );
}

Protected void bindmaterieldropdownlist (gridviewrow row, string contractno)
{
Dropdownlist ddlmaterielno = (dropdownlist) Row. cells [2]. findcontrol ("ddlmaterielno ");
Ddlmaterielno. datasource = getallmaterielbycontractno (contractno );
Ddlmaterielno. datatextfield = "no ";
Ddlmaterielno. databind ();
}
}

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.