【问题标题】:How to keep the checkbox inside gridview checked based on certain condition?如何根据特定条件检查gridview中的复选框?
【发布时间】:2014-06-06 15:31:56
【问题描述】:

我的 web 上有一个 gridview 和模板字段内的复选框。我在数据库中有一个字段,其中包含整数值 0 或 1。0 表示启用,1 表示禁用。当我选中我的复选框时,它会为数据库中的特定字段插入 1,反之亦然。现在我希望当我打开此页面时,表中值为 1 的行应保持选中状态,表中值为 0 的行应保持未选中状态。我试过这样做 - 这是我的 aspx 页面-

<asp:GridView ID="GridMain" runat="server" Width="1000px" 
        AutoGenerateColumns="False" onrowdatabound="GridMain_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="Student Name">
                <ItemTemplate>
                    <asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Enable/Disable">
                <ItemTemplate>
                    <asp:CheckBox ID="chkenbl" runat="server" AutoPostBack="True" 
                        oncheckedchanged="chkenbl_CheckedChanged" 
                        Checked='<%# Convert.ToBoolean(Eval("en_dis")) %>' />
                    <br />
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                    &nbsp;<asp:Label ID="Label2" runat="server" Text='<%# Eval("en_dis") %>' 
                        Visible="False"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

这是我的cs页面-

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            show();

        }
        //chkbind();
    }
    public void show()
    {
        try
        {
            dt = g1.return_dt("select id,name,en_dis from tbl_data_show order by name");
            if (dt.Rows.Count > 0)
            {
                adsource = new PagedDataSource();
                adsource.DataSource = dt.DefaultView;
                adsource.PageSize = 5;
                GridMain.DataSource = adsource;
                GridMain.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
protected void chkenbl_CheckedChanged(object sender, EventArgs e)
    {
            CheckBox chk = (CheckBox)sender;
            //CheckBox chk = sender as CheckBox;
            //CheckBox chk = (CheckBox)row.FindControl("chkenbl");
            GridViewRow row = (GridViewRow)chk.NamingContainer;
            if (chk.Checked)
            {
                try
                {
                    //Label lblid = (Label)GridMain.FindControl("Label1");
                    //Label lblid = new Label();
                    //lblid.Text = GridMain.FindControl("Label1").ToString();
                    string lblid = ((Label)row.FindControl("Label1")).Text;
                    rows = g1.ExecDB("update tbl_data_show set en_dis='1' where id=" + lblid);
                    if (rows > 0)
                    {
                        Response.Write("Rows Effected Successfull.");
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
            else
            {
                //Label lblid1 = (Label)GridMain.FindControl("Label1");
                //Label lblid1 = new Label();
                //lblid1.Text = GridMain.FindControl("Label1").ToString();
                string lblid1 = ((Label)row.FindControl("Label1")).Text;
                rows = g1.ExecDB("update tbl_data_show set en_dis='0' where id=" + lblid1);
                if (rows > 0)
                {
                    Response.Write("Rows Effected Successfull.");
                }
            }
    }
protected void GridMain_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dt = g1.return_dt("select en_dis from tbl_data_show");
            if (dt.Rows.Count > 0)
            {
               // CheckBox chk1 = new CheckBox();
                CheckBox chk1 = (CheckBox)e.Row.FindControl("chkenbl");
                //CheckBox
                if (dt.Rows[0]["en_dis"] == "0")
                {

                    chk1.Checked = false;
                }
                else
                {
                    chk1.Checked = true;
                }
            }
        }
    }

请指导我哪里做错了?

【问题讨论】:

    标签: c# asp.net gridview checkbox


    【解决方案1】:

    您可以在标记中添加Checked='&lt;%# Convert.ToBoolean(Eval("en_dis")) %&gt;'。请参见下面的示例:

    <asp:CheckBox ID="chkenbl" runat="server" AutoPostBack="True" Checked='<%# Convert.ToBoolean(Eval("en_dis")) %>' oncheckedchanged="chkenbl_CheckedChanged" />
    

    1. 使用OnRowDataBound gridview 事件。
    2. 使用相同的 Eval 函数创建一个 asp 隐藏字段以保留数据表的 en_dis 列的值。
    3. 找到 Checkbox 控件并根据从隐藏字段控件中检索到的值将其选中或取消选中。

    【讨论】:

    • 按照您的建议,我对代码进行了更改。请检查。但我收到此错误-指定的演员表无效。
    • @Omi。如果您在 markup 中进行更改,那么您应该尝试更新的解决方案。如果 null 是可能的值,而不是 01,那么您必须使用 Convert.ToBoolean
    • 我在表的 en_dis 字段中仅存储 1 或 0。 1-如果选中复选框。 0- 如果未选中复选框
    • 你在标记中尝试过这个Checked='&lt;%# Convert.ToBoolean(Eval("ColumnValue")) %&gt;'而不使用OnRowDataBound事件吗?
    • 实际上您正在实施这两种解决方案。您必须在标记中设置复选框,或者您可以使用OnRowDataBound。如果您只需要在01 的基础上做出决定,那么您不需要使用OnRowDataBound。如果您必须根据列值执行一些复杂的任务,那么您可以使用事件。在这种情况下,您不应该使用标签来保持价值,而是使用asp hidden field
    【解决方案2】:

    您不能在页面加载事件中选中复选框,您必须像这样使用 rowdatabound

     protected void GridViewRowEventHandler(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.DataRow)
            {
     //here comes your code for checking or make it unchecked   
        }
        }
    

    在asp中你可以做到这一点

    <asp:GridView OnRowDataBound="GridViewRowEventHandler"....>
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      要根据存储在数据库中的值首先检查复选框,您需要在数组或数据表等中获取这些值。 那么在数据绑定到gridview之后,你需要使用这个代码。

      for (int i = 0; i < gvShowReport.Rows.Count; i++)
               {
                   if(vdt.Rows[i]["status"].ToString()=="True")
                   {
                       CheckBox CheckRow = ((CheckBox)gvShowReport.Rows[i].FindControl("cbCheckRow"));
                       CheckRow.Checked = true;
                   }
                   else
                   {
                       CheckBox CheckRow = ((CheckBox)gvShowReport.Rows[i].FindControl("cbCheckRow"));
                       CheckRow.Checked = false;
                   }
               }
      

      gvShowReport 是 gridview 控件的 id,vdt 是包含状态列的数据表,值为 0(False) 和 1(True)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-09
        • 2017-12-21
        • 2021-08-12
        • 2020-01-10
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多