Click here to Skip to main content
14,980,146 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
 {
  for (int i = 0; i < GridView1.Rows.Count; i++)
 {
              RadioButtonList rbAtt = (RadioButtonList)GridView1.Rows[i].Cells[3].FindControl("rbAtt");
   if (rbAtt.SelectedIndex != null)
     {
     string sid = GridView1.Rows[i].Cells[0].Text;
     con.Open();
     SqlCommand cmd = new SqlCommand("InsertAttendence", con);
     cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Date", (DateTime.Now.Month + DateTime.Now.Year).ToString());
   cmd.Parameters.AddWithValue("@StudentId", sid);
  if (dlAtt.SelectedItem.Text == "Morning")
     {
       cmd.Parameters.AddWithValue("@Morning", rbAtt.SelectedValue);
      }
      else
      {
         cmd.Parameters.AddWithValue("@Morning", DBNull.Value);
       }
      if (dlAtt.SelectedItem.Text == "Evening")
      {
        cmd.Parameters.AddWithValue("@Evening", rbAtt.SelectedItem.Value);
       }
        else
     {
        cmd.Parameters.AddWithValue("@Evening", DBNull.Value);
        }
        cmd.Parameters.AddWithValue("@PostedOn", DateTime.Now.ToLocalTime());
        cmd.Parameters.AddWithValue("@PostedBy", Session["StaffId"].ToString());
        cmd.ExecuteNonQuery();
        con.Close();

          }
      }


if (rbAtt.SelectedIndex != null) in this line at every selection it will take index value is -1 only.

This is my ASPX code:

<pre lang="ASPX">
 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None" AutoGenerateColumns="False"
           >
            <Columns>
            <asp:BoundField DataField="AdmissionNo" HeaderText="AdmissionNo" />
            <asp:BoundField DataField="RollNo" HeaderText="StudentId" />
            <asp:BoundField DataField="FullName" HeaderText="StuName" />
            <asp:TemplateField HeaderText="Attendence">
            <ItemTemplate>
          <asp:RadioButtonList ID="rbAtt" runat="server">
           <asp:ListItem Value="p">Present</asp:ListItem>
          <asp:ListItem Value="A">Absent</asp:ListItem>
          <asp:ListItem Value="L">Leave</asp:ListItem>
        </asp:RadioButtonList>
            </ItemTemplate>
            
            </asp:TemplateField>
            
            </Columns>
        </asp:GridView>
Posted

1 solution

Hi,

By default RadioButtonList will give -1 index only, so you want to give by default one value should be selected like below.

XML
<asp:TemplateField HeaderText="Attendence">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="rbAtt" runat="server">
                            <asp:ListItem Text="Present" Selected="True" Value="p"></asp:ListItem>
                            <asp:ListItem Text="Absent" Value="A"></asp:ListItem>
                            <asp:ListItem Text="Leave" Value="L"></asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
   
Comments
shwetha1 27-Apr-12 8:02am
   
Thanks for u r reply but at this time it will take selected index value always 0....
I want in this way: When i clicking on different buttons it will choose different indexes..

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