views:

30

answers:

2

Please help me out I am having a Gridview named GridViewGender with a RadioButtonList with it Inline code is

<asp:GridView ID="GridViewGender" runat="server"
        AutoGenerateColumns="False"  
       Width="494px" DataKeyNames="ren">

    <Columns>

        <asp:BoundField DataField="ren" HeaderText="Items" />

        <asp:TemplateField HeaderText="Attendance" >
            <ItemTemplate>

                <asp:RadioButtonList  ID="RadioButtonListGender" runat="server" Enabled="true"
                     RepeatDirection="Horizontal" TextAlign="Left"  
                      >
                <asp:ListItem Value="1">Male</asp:ListItem>
                <asp:ListItem Value="2">Female</asp:ListItem>                       

                </asp:RadioButtonList>
            </ItemTemplate>

        </asp:TemplateField>
    </Columns>
</asp:GridView> 

 protected void Page_Load(object sender, EventArgs e)
    {
        MyClass obj = new MyClass ();
        GridViewGender.DataSource = obj.check();
        GridViewGender.DataBind();
    }

protected void Button1_Click(object sender, EventArgs e)
{
    List<string> checking = new List<string>();
    for(int i = 0 ; i < GridViewGender.Rows.Count ; i ++)
    {
        GridViewRow row = GridViewGender.Rows[i];
         string  rr = Convert.ToString( ((RadioButtonList)row.FindControl("RadioButtonListGender")).SelectedItem.Text)   ;
         checking .Add((string.IsNullOrEmpty(rr)) ? string.Empty : rr);
    }
}
A: 

It looks like you're rebinding the GridView each time you do a page load, so the user's selections will likely get overwritten anyway.

As requested.

Joel Etherton
A: 

How about the good old !IsPostBack for binding

protected void Page_Load()
{
    if (!IsPostBack)
    {
       // Binding code
    }
}

and using EnableViewState="true" on your GridView declaration so the data is not rebound on each page load.

Also, if you're having problems casting, you could try casting with row.Cells[1].Controls[1] as RadioButtonList instead of FindControl.

Brissles