【问题标题】:check checkbox inside gridview either it is checked or not?选中gridview中的复选框是否已选中?
【发布时间】:2013-03-25 04:54:27
【问题描述】:

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

每次都进入最后一个 else...为什么?

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    改变

    if (chk != null & chk.Checked)
    

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

    编辑基于 OP 的 cmets

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

    if(!Page.IsPostBack)
    {
           //bind here
    } 
    

    【讨论】:

    • 仍然无法正常工作.....也就是说,如果我选中了该框,那么它也被视为未选中.....
    • 你确定你的 chk.Checked 是真的吗?
    • 然后它应该去else块,你调试代码了吗?您可能会得到最后一行gridview的结果,更改最后一行gridview的复选框看看会发生什么?
    • 亲爱的,我认为您根本没有得到我的问题...我有 if else 条件。如果条件匹配然后执行 action-1,否则执行 action-2...所以我的问题是我我没有进入行动1,也就是说,如果我选中复选框而不是它应该通过 IF 块..但它每次都通过 ELSE 块。即使我选中了复选框,它也认为它是未选中的,我的问题为什么会这样?我在调试代码时才知道这个问题。
    • 感谢您解开困惑,您能否检查一下是否在 if(!Page.IsPostBack){//bind here} 中绑定了网格,就像您在回发时再次绑定网格一样,您将失去即使您在回发之前检查了网格和 checkbox1 中的控件,它也会为 false。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 2015-06-30
        • 2016-06-12
        • 2018-10-25
        • 2016-08-20
        • 2022-11-02
        • 2017-06-07
        • 2013-02-15
        • 2014-03-02
        • 1970-01-01
        相关资源
        最近更新 更多