escobar_season
escobar_season

Reputation: 113

Make a DropdownList visible on click of a RadioButton

When I perform a radiobuttonclick, I want to set a dropdownlist to become visible. The radiobutton and dropdownlist are within the same datagrid. I am not sure how to do this.

<asp:UpdatePanel ID="updatepanel" UpdateMode="conditional" runat="server">
    <ContentTemplate>
        <asp:DataGrid ID="DataGrid" AutoGenerateColumns = "false" CssClass="objectSubTitle" ItemStyle-Wrap="true" runat="server" OnItemCommand="handler" ><Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                   <asp:RadioButton ID ="RadioButton1" Text="Yes" GroupName="creatingNewCard" OnCheckedChanged="RadioButtonYes" AutoPostBack="True" runat="server" />
                  <asp:DropDownList ID="DropDownList1"  Visible="false" runat="server"/>
                </ItemTemplate>
            </asp:TemplateColumn>

Upvotes: 1

Views: 3190

Answers (2)

HatSoft
HatSoft

Reputation: 11201

You can call the Visible property of the DropdwonList in the radio buttons checked changed event like this

protected void RadioButton1_CheckedChanged(Object sender, EventArgs e)
{
    var radioButton1= (RadioButton)sender;
    var item = (DataGridItem)radioButton1.NamingContainer;
    var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");

    dropDownList1.Visible = radioButton1.Checked ? true : false;
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460238

Assuming that they are in an ItemTemplate of a TemplateField and you want to switch vivibility on serverside:

protected void RbCheckedChanged(Object sender, EventArgs e)
{
    var radioButton1 = (RadioButton)sender;
    var row = (GridViewRow)radioButton1.NamingContainer;
    var dropDownList1 = (DropDownList)row.FindControl("DropDownList1");
    dropDownList1.Visible = radioButton1.Checked;
}

Sample-GridView:

<asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="Grid_RowDataBound"
    runat="server">
    <Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:RadioButton  ID="RadioButton1" runat="server" OnCheckedChanged="RbCheckedChanged" AutoPostBack="true"></asp:RadioButton>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

Edit: as you've edited your question to show that you really use a DataGrid instead of a GridView, the code is similar:

protected void RbCheckedChanged(Object sender, EventArgs e)
{
    var radioButton1 = (RadioButton)sender;
    var item = (DataGridItem)radioButton1.NamingContainer;
    var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
    dropDownList1.Visible = radioButton1.Checked;
}

Upvotes: 2

Related Questions