简体   繁体   中英

how to change drop down list based on gridview selection

I have a drop down list which will load up with a series of customer ID numbers. I then have a gridview control on my page with the select hyperlink. When i click on the link to select the row in gridview i would like my dropdown to change to that number.

Below is what i have tried but doesn't work:

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    if (!GridView1.SelectedIndex.Equals(-1))
    {
        DropDownList ddl;

        ddl = (DropDownList)form1.FindControl("ddl_Customers");
        ddl.SelectedValue = (String)GridView1.SelectedDataKey.Values[0];
    }
}

Handle the SelectedIndexChanged event for GridView1

void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
    ddl_Customers.SelectedValue = GridView1.SelectedDataKey.Value.ToString();
}

You can directly use GridView1.SelectedValue.ToString() for this. To use that, you should define a datakeyname like this: <asp:Gridview DataKeyNames="CustomerID">

Then all you need is this:

void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
    ddl_Customers.SelectedValue = GridView1.SelectedValue.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM