为什么我的gridview不接受单选按钮列表 [英] why my gridview not accepting radio button list
本文介绍了为什么我的gridview不接受单选按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友....
我想要带有RadioButton模板字段的GridView.为此,我编写了以下代码:
Hi Friends....
I want GridView with RadioButton template field. for that I write the below code:
<pre lang="ASPX">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="AdmissionNo" HeaderText="AdmNo" />
<asp:BoundField DataField="RollNo" HeaderText="RollNo" />
<asp:BoundField DataField="FullName" HeaderText="Name" />
<asp:TemplateField HeaderText="Attandance">
<ItemTemplate>
<asp:RadioButtonList ID="rbAtt" runat="server">
<asp:ListItem Text="Present" 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></Columns>
</asp:GridView>
但是当我运行程序时,此GridView不接受此RadioButton,它将返回此值为null.
But when I run the program this GridView is not accept this RadioButton it returns this value is null..
BindGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select AdmissionNo,FullName,RollNo from Student_Full where PresentClass='1st Class'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
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.SelectedItem == null)
{
lblatt.Text = "Select student Attendance";
}
if (rbAtt.SelectedItem != 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);
cmd.Parameters.AddWithValue("@Evening", rbAtt.SelectedItem.Value);
cmd.Parameters.AddWithValue("@PostedOn", DateTime.Now.ToLocalTime());
cmd.Parameters.AddWithValue("@PostedBy", Session["StaffId"].ToString());
cmd.ExecuteNonQuery();
con.Close();
lblatt.ForeColor = System.Drawing.Color.Green;
lblatt.Text = "your attendance is posted successfully.......";
}
}
}
任何一个 pls 都可以帮助我...
any one pls please help me...
推荐答案
您在哪里调用gridview的绑定方法?我认为您在pageload事件中完成了所有绑定代码.验证是否绑定方法是在每次回发时调用,如果是,则将其更改:-
where you are calling binding method of gridview ? i think you done your all binding code inside pageload event. verify is binding method is calls on every postback if yes then change it :-
if (!Page.IsPostBack)
{
//yuor binding code
}
< asp:radiobutton id ="rd" runat ="server" text ="m">
<asp:radiobutton id="rd" runat="server" text="m">
For Each row As GridViewRow In GridView2.Rows
dim rb as RadioButton
rb = DirectCast(GridView2.Rows(row.RowIndex).FindControl("rb1"), RadioButton)
If rb .Checked = True Then
Response.Clear()
'here what we want to perporm tht code to write
Response.Write("checked RadioButton: " + Convert.ToString(row.RowIndex))
'Else
' Response.Write("Not checked any RadioButton in grid")
End If
Next
这篇关于为什么我的gridview不接受单选按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文