Search code examples
asp.netvb.netgridviewradiobuttonlist

Need help setting the same values for radiobutton list in gridview in ItemTemplate based on selected value radiobuttion list in HeaderTemplate


i've been tasked to create a Gridview in which a user click a radiobutton list on the header (rblDifficulty) and all the radiobutton list in the rows below (rblDiff) will be set to the same values. I've looked and modify examples using checkboxes and jquery but without success. Hope someone can guide me on this

 <asp:GridView ID="gvData" runat="server" EmptyDataText="No Data" DataKeyNames="ID"  AllowPaging="true" PageSize ="15"
        class="" AutoGenerateColumns="False" OnPageIndexChanging="OnPaging">
        <Columns>

            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>                                  
            <asp:TemplateField HeaderText="Difficulty">
              <HeaderTemplate>
              <asp:RadioButtonList  ID="rblDifficulty"  runat="server">
                        <asp:ListItem Text="Easy" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Medium" Value="Y"></asp:ListItem>
                        <asp:ListItem Text="Hard" Value="F"></asp:ListItem>
              </asp:RadioButtonList>
              </HeaderTemplate> 
                <ItemTemplate>
                    <asp:RadioButtonList ID="rblDiff" runat="server">
                        <asp:ListItem Text="Easy" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Medium" Value="Y"></asp:ListItem>
                        <asp:ListItem Text="Hard" Value="F"></asp:ListItem>
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>

UPDATE: jomsk1e helped me on this

 Protected Sub rblDifficulty_onChanged(sender As Object, e As EventArgs)
    Dim rblDifficulty As RadioButtonList = DirectCast(gvData.HeaderRow.FindControl("rblDifficulty"), RadioButtonList)
    Dim selectedValue__1 As String = rblDifficulty.SelectedValue

    For Each row In gvData.Rows
        Dim rbl As RadioButtonList = DirectCast(row.FindControl("rblDiff"), RadioButtonList)
        rbl.SelectedValue = selectedValue__1
    Next
End Sub

Solution

  • Try this, using server side processing:

    <asp:RadioButtonList  ID="rblDifficulty"  runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblDifficulty_onChanged">
        <asp:ListItem Text="Easy" Value="0"></asp:ListItem>
        <asp:ListItem Text="Medium" Value="Y"></asp:ListItem>
        <asp:ListItem Text="Hard" Value="F"></asp:ListItem>
    </asp:RadioButtonList>
    
    protected void rblDifficulty_onChanged(object sender, EventArgs e)
    {
        RadioButtonList rblDifficulty = (RadioButtonList)gvData.HeaderRow.FindControl("rblDifficulty");
        string selectedValue = rblDifficulty.SelectedValue;
    
        foreach (gvData row in gvData.Rows)
        {
            RadioButtonList rbl = (RadioButtonList)row.FindControl("rblDiff");
            rbl.SelectedValue = selectedvalue;
        }
    }
    

    Not tested, but will definitely give you a hint how to do it. Good luck! :)