为什么我选择的索引值在单选按钮列表中没有变化 [英] why my selected index value is not changing in radio button list

查看:77
本文介绍了为什么我选择的索引值在单选按钮列表中没有变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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),它将采用的索引值仅为-1.

这是我的ASPX代码:



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>

推荐答案



默认情况下,RadioButtonList仅给出-1索引,因此默认情况下要选择一个值,如下所示.

Hi,

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

<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>


这篇关于为什么我选择的索引值在单选按钮列表中没有变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆