簡體   English   中英

檢查gridview內的復選框是否已選中?

[英]check checkbox inside gridview either it is checked or not?

我有gridview,其中包含復選框作為模板字段。 我已經嘗試過如此努力,但我仍然無法獲得所需的結果,即如果選中復選框,則執行操作-1,否則執行操作-2,但每次執行操作-2時。 以下是我的代碼,我需要你的幫助。

Gridview代碼:

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

每次它進入最后的其他...為什么?

使用logical和operator &&而不是bitwise & operator來組合if語句中的條件。

更改

if (chk != null & chk.Checked)

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

根據OP的評論進行編輯

您需要檢查綁定網格的方式是否在回發時沒有綁定。

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

確保在頁面加載時不再綁定gridview,如果是,則必須應用IsPostback檢查

試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM