簡體   English   中英

如何根據一定的條件保持選中的網格視圖中的復選框?

[英]How to keep the checkbox inside gridview checked based on certain condition?

我在我的網站上有一個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;
                }
            }
        }
    }

請指導我我做錯了什么?

在標記中,您可以添加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. 查找復選框控件,並根據從隱藏字段控件中檢索到的值將其選中或取消選中。

您不能在頁面加載事件中選中復選框,您必須像這樣使用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"....>

希望對你有幫助

要根據存儲在數據庫中的值選中該復選框,首先需要在數組或數據表等中獲取這些值。然后將數據綁定到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)

暫無
暫無

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

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