我有 gridview,其中包含作为模板字段的复选框。我已经很努力了,但我仍然无法获得想要的结果,也就是说,如果复选框被选中,则执行 action-1,否则执行 action-2,但每次它都在执行 action-2。下面是我的代码,我几乎不需要你的帮助。
网格视图代码:
<asp:GridView ID="final" runat="server" AutoGenerateColumns="False";>
<Columns>
<asp:BoundField DataField="name" HeaderText="Employee Name" SortExpression="date" />
<asp:BoundField DataField="ldate" HeaderText="Date Of Leave" SortExpression="ldate"
/>
<asp:TemplateField HeaderText="Half/Full">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Enabled="true" Value="Half">Half</asp:ListItem>
<asp:ListItem Enabled="true" Value="Full">Full</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我为检查收音机和复选框所做的代码:
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Date", typeof(DateTime)));
dtable.Columns.Add(new DataColumn("Half/Full", typeof(float)));
dtable.Columns.Add(new DataColumn("Status", typeof(string)));
Session["dt"] = dtable;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
foreach (GridViewRow gvrow in final.Rows)
{
dtable=(DataTable)Session["dt"];
CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
if (chk != null & chk.Checked)
{
RadioButtonList rButton = (RadioButtonList)gvrow.FindControl("RadioButtonList1");
if (rButton.SelectedValue == "Half")
{
//perform action-1
}
else
{
//perform action-1
}
}
else
{
perform action-2
}
}
每次它进入最后一个其他...为什么?