3

我有 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
     }
}

每次它进入最后一个其他...为什么?

4

3 回答 3

4

使用逻辑和运算符&&而不是按位运算&符来组合 if 语句中的条件。

改变

if (chk != null & chk.Checked)

if (chk != null && chk.Checked)

根据OP的评论进行编辑

您需要检查是否以未在回发时绑定网格的方式绑定网格。

if(!Page.IsPostBack)
{
       //bind here
} 
于 2013-03-25T04:55:53.603 回答
0

确保您没有在页面加载时再次绑定 gridview,如果是,则必须应用 IsPostback 检查

于 2013-03-25T05:52:50.267 回答
0

尝试这个

<asp:CheckBox ID="CheckBox1" runat="server"  OnCheckedChanged="CheckBox1_CheckedChanged"  AutoPostBack="true"/>

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow gr = (GridViewRow)chk.Parent.Parent;
    RadioButtonList RadioButtonList1 = (RadioButtonList)gr.FindControl("RadioButtonList1");
    if (RadioButtonList1 != null)
    {
        RadioButtonList1.Items.FindByText("Full").Selected = true;
    }       
}
于 2013-03-25T11:09:14.420 回答