Handling Dropdown event within GridView

Consider a GridView that contains three columns: First Name, Alien Dropdown and Country Dropdown. The Alien Dropdown has two options, Yes and No. Select Yes in the Alien Dropdown. The Country Dropdown is enabled.

Both Dropdowns are defined in a TemplateColumn. For this example, we bind the GridView to a XmlDataSource.

<asp:Gridview ID="Grid1" runat="server" 
DataSourceID="NameXml" 
AutoGenerateColumns="false"
OnRowDataBound = "Grid1_RowDataBound" >
<Columns>
<asp:BoundField DataField="FirstName" />
<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="AlienList" runat="server" 
        AutoPostBack="true"
        OnSelectedIndexChanged="AlienList_SelectedIndexChanged" >
            <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
            <asp:ListItem Text="No" Value="N"></asp:ListItem>
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <asp:DropDownList ID="CountryList" runat="server">
            <asp:ListItem Text="" Value=""></asp:ListItem>
            <asp:ListItem Text="USA" Value="USA"></asp:ListItem>
            <asp:ListItem Text="UK" Value="UK"></asp:ListItem>
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
<asp:XmlDataSource ID="NameXml" runat="server" DataFile="Names.xml">
    
</asp:XmlDataSource>

Handle the RowDataBound event of the GridView. In the event handler, we check if Alien Dropdown has a value of Yes. If Yes, we enable the Country Dropdown.

protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList lstAlien = e.Row.FindControl("AlienList") as DropDownList;

        // DataItem is a XmlDataSourceNodeDescriptor
        // To get attribute, use XPathBinder
        lstAlien.SelectedValue = XPathBinder.Eval(e.Row.DataItem, "@Alien").ToString();

        DropDownList lstCountry = e.Row.FindControl("CountryList") as DropDownList;
        if (lstAlien.SelectedValue == "Y")
        {
            lstCountry.Enabled = true;
            lstCountry.SelectedValue = XPathBinder.Eval(e.Row.DataItem, "@Country").ToString();
        }
        else
        {
            lstCountry.Enabled = false;
        }
    }    
}

Each item in a XmlDataSource is a XmlDataSourceNodeDescriptor. To get the XmlNode attributes, use the XPathBinder. Finally, we handle the OnSelectedIndexChange event of the Alien Dropdown.

protected void AlienList_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList lstAlien = (DropDownList)sender;
    GridViewRow row = lstAlien.NamingContainer as GridViewRow;
    DropDownList lstCountry = row.FindControl("CountryList") as DropDownList;
    if (lstAlien.SelectedValue == "Y")
    {
        lstCountry.Enabled = true;
    }
    else
    {
        lstCountry.SelectedValue = "";
        lstCountry.Enabled = false;
    }
}

To get the GridViewRow where the Dropdown is present, use NamingContainer property of the control. Use  FindControl method to find the Country Dropdown. Enable or Disable the Country Dropdown based on the value in the Alien Dropdown.

Related Posts

Leave a Reply

Your email address will not be published.