Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have grid and in each row there are 4 radiobutton.i have initially set the first radio of a row is checked=true and rest 3 false.
now the problem is i can not determine in code that which radio is checked from each row of the gridview. every time in code it is showing that first radio is checked.thus i am gettin the wrong value

--------------------------------------------------

ASP.NET
<table width="100%">
                                        <tr>
                                            <td>
                                                <asp:UpdatePanel ID="UpdatePanel11" runat="server">
                                                    <ContentTemplate>
                                                        <asp:GridView ID="dgvAttandance" runat="server" AutoGenerateColumns="false" DataKeyNames="StffId"
                                                            Width="100%" CssClass="style5">
                                                            <Columns>
                                                                <asp:TemplateField HeaderText="Stff Id" HeaderStyle-HorizontalAlign="Center">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblStffId" runat="server" Text='<%# Bind("StffId")%>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Center">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Date Time" HeaderStyle-HorizontalAlign="Center">
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblDT" runat="server" Text='<%# Bind("DT")%>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Attendance">
                                                                    <ItemTemplate>
                                                                        <asp:RadioButton ID="rdoP" Text="P" ForeColor="Green" Font-Bold="true" runat="server"
                                                                            Checked="true" GroupName="Att" />
                                                                         
                                                                        <asp:RadioButton ID="rdoA" Text="A" ForeColor="Blue" Font-Bold="true" runat="server"
                                                                            Checked="false" GroupName="Att" />
                                                                         
                                                                        <asp:RadioButton ID="rdoS" Text="S" ForeColor="Red" Font-Bold="true" runat="server"
                                                                            Checked="false" GroupName="Att" />
                                                                         
                                                                        <asp:RadioButton ID="rdoH" Text="H" ForeColor="Orange" Font-Bold="true" runat="server"
                                                                            Checked="false" GroupName="Att" />
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                            </Columns>
                                                        </asp:GridView>
                                                    </ContentTemplate>
                                                </asp:UpdatePanel>
                                            </td>
                                        </tr>
                                    </table>


---------------------------------------
C#
for (int index = 0; index <= dgvAttandance.Rows.Count - 1; index++)
           {
               char stat = new char();
               RadioButton P = (RadioButton)dgvAttandance.Rows[index].FindControl("rdoP");
               RadioButton A = (RadioButton)dgvAttandance.Rows[index].FindControl("rdoA");
               RadioButton S = (RadioButton)dgvAttandance.Rows[index].FindControl("rdoS");
               RadioButton H = (RadioButton)dgvAttandance.Rows[index].FindControl("rdoH");
               if (P.Checked)
               {
                   stat = Convert.ToChar("P");
               }
               else if (A.Checked)
               {
                   stat = Convert.ToChar("A");
               }
               else if (S.Checked)
               {
                   stat = Convert.ToChar("S");
               }
               else if (H.Checked)
               {
                   stat = Convert.ToChar("H");
               }

               DT.Rows.Add(((Label)dgvAttandance.Rows[index].FindControl("lblStffId")).Text, ((Label)dgvAttandance.Rows[index].FindControl("lblName")).Text, stat);
           }
Posted
Comments
agent_kruger 23-May-14 9:51am    
sir, i still cant get it, whats the problem with the below code in c#.
BIBASWAN 23-May-14 10:13am    
this code seems fine but the current value of radio button which is chaked i cannot ditermine from the code
agent_kruger 24-May-14 6:35am    
sir, what is the problem as you check through radiobuttons value, please debug and let us know.
j snooze 23-May-14 17:22pm    
Not sure where your radio button check code is, but is it possible you are rebinding your data on the page load everytime? Make sure in the page load event you are doing an
if(!IsPostBack){
bindGridToData()
}

That way it doesn't rebind all the data and reset everything everytime you push a button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900