2015-02-11 155 views
0

我在gridview中綁定了一個數據庫字段的複選框,該數據庫字段存儲布爾值,即IsRejected。我試圖讓它實時,即如果我勾選複選框,那麼它應該把數據庫中的1或0,我已經嘗試過但沒有效果。爲什麼?爲什麼gridview中的複選框不起作用?

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 

             <ContentTemplate> 
              <div> 
               <asp:GridView ID="grdvwRejectedEmployees" runat="server" AutoGenerateColumns="false" Width="100%" 
              DataKeyNames="EdrID" CssClass="table table-hover table-striped table-bordered" 
              AllowPaging="true" AllowSorting="true"> 

              <Columns> 
              <asp:BoundField DataField="EdrID" HeaderText="Edr ID" /> 
              <asp:BoundField DataField="EmployerName" HeaderText="Employer" /> 
              <asp:BoundField DataField="BranchName" HeaderText="Branch" /> 
              <asp:BoundField DataField="EmployeeUniqueID" HeaderText="EmployeeUniqueID" /> 
              <asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" /> 
              <asp:BoundField DataField="PayStartDate" HeaderText="Pay Start Date" /> 
              <asp:BoundField DataField="PayEndDate" HeaderText="Pay End Date" /> 
              <asp:TemplateField HeaderStyle-CssClass="visible-desktop" ItemStyle-CssClass="visible-desktop"> 
               <ItemTemplate> 
                <asp:CheckBox ID="chkBoxIsRejection" runat="server" Text='<%# Bind("IsRejected") %>' /> 
               </ItemTemplate> 
              </asp:TemplateField> 
              <%--<asp:BoundField DataField="IsRejected" HeaderText="Is Rejected" />--%> 
              </Columns> 

             </asp:GridView> 
              </div> 
             </ContentTemplate> 

            </asp:UpdatePanel> 

的.cs

if (!IsPostBack) 
     { 

      if (Session["UserCredential"] != null) 
      { 
       ClsSystemUser user = (ClsSystemUser)Session["UserCredential"]; 
       ClsSystemUser obj = new ClsSystemUser(); 
       obj = obj.GetOrganizationName(user.Type_ID, user.OfficeID, user.SystemUserID); 

       int usertypeid = user.Type_ID; 


       if (usertypeid == 3) 
       { 
        //UserTypeID.Text = "Branch"; 

        ClsRejectedFiles RejFiles = new ClsRejectedFiles(); 
        grdvwRejectedEmployees.DataSource = RejFiles.GetRejectedFiles("30411098037111", 3, 1); 
        grdvwRejectedEmployees.DataBind(); 

       } 

一樣,如果我打勾或取消勾選複選框,然後它不會影響數據庫,即價值,如果我籤的話,我應該把1數據庫IsRejected列或0如果我取消選中

+1

選中複選框的'Checked'財產與您的數據字段代碼。您正在設置Text屬性。 – 2015-02-11 07:17:35

回答

2

您正在設置複選框的Text屬性與數據字段值嘗試設置Checked屬性,以便它將設置複選框值。

<asp:CheckBox 
     ID="chkBoxIsRejection" runat="server" HeaderText="IsRejection" 
     Checked='<%#Convert.ToBoolean(Eval("IsRejected")) %>'/> 

UPDATE

歐普想更新chagned上覆選框檢查數據庫。爲此,您需要添加事件以告知頁面如何在複選框選中時發生反應

您需要添加事件CheckedChangedYou can read it here.

 <asp:CheckBox 
     ID="chkBoxIsRejection" runat="server" HeaderText="IsRejection" 
     Checked='<%#Convert.ToBoolean(Eval("IsRejected")) %>' 
     AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged"/> 

然後在後面

 protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
    { 
      // write code to save the changes to the database. 
    } 
+0

不起作用,同樣的問題,但至少它代表真正的價值,但不實時工作 – 2015-02-11 07:28:40

+0

你的意思是什麼,實時不工作? – 2015-02-11 07:29:18

+0

如果我勾選或取消選中複選框,那麼它不會影響數據庫中的值,即如果我檢查那麼我應該把1放在數據庫IsRejected列或0如果我取消選中 – 2015-02-11 07:39:09

相關問題