如何根据特定条件检查gridview中的复选框?

时间:2014-04-22 10:10:32

标签: c# asp.net gridview checkbox

我的网络上有一个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;
                }
            }
        }
    }

请指导我在哪里做错了?

3 个答案:

答案 0 :(得分:1)

在标记中,您可以添加Checked='<%# Convert.ToBoolean(Eval("en_dis")) %>'。见下面的例子:

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

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

答案 1 :(得分:0)

您无法在页面加载事件中选中复选框,您必须像这样使用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"....>

希望能帮到你

答案 2 :(得分:0)

要根据数据库中存储的值首先检查复选框,您需要在数组或数据表等中获取这些值。 然后在数据绑定到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)

相关问题