【问题标题】:Why a checkbox in a gridview doesn't work?为什么gridview中的复选框不起作用?
【发布时间】:2015-04-11 11:33:53
【问题描述】:

我在 gridview 中将一个复选框与一个数据库字段绑定,该字段存储布尔值,即 IsRejected。我正在尝试使其成为实时,即如果我勾选复选框,那么它应该将 1 放入数据库或 0 中,我尝试过但没有效果。为什么?

 <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();

                }

如果我勾选或取消勾选复选框,那么它不会影响数据库中的值,即如果我检查,那么我应该在数据库 IsRejected 列中输入 1,如果我取消选中则输入 0

【问题讨论】:

  • 使用您的数据字段设置复选框的Checked 属性。您正在设置 Text 属性。

标签: c# asp.net c#-4.0 gridview checkbox


【解决方案1】:

您正在使用数据字段值设置复选框的Text 属性尝试设置Checked 属性,以便设置复选框值。

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

更新

由于 OP 想要在复选框检查更改时更新数据库。为此,您需要添加事件以告诉页面在选中的复选框更改时如何做出反应

您需要添加事件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.
     }

【讨论】:

  • 不起作用,同样的问题,但至少它代表了真正的价值,但不能实时工作
  • 如果我勾选或取消勾选复选框,那么它不会影响数据库中的值,即如果我检查,那么我应该在数据库 IsRejected 列中输入 1,如果我取消选中则输入 0
猜你喜欢
  • 2022-01-24
  • 2016-01-14
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多