簡體   English   中英

如果選中gridview內的復選框,如何在數據庫字段中插入0或1?

[英]How to insert 0 or 1 in a database field if checkbox inside the gridview is checked?

我的Web表單上有一個gridview,並且我在gridview的模板字段中選中了復選框。 我希望如果在gridview中選中該復選框,它將在我的數據庫列中插入1,如果未選中,則應插入0,即默認情況下該字段應插入0。我已經嘗試過這樣做-My aspx頁面 -

<asp:GridView ID="GridMain" runat="server" Width="1000px" 
        AutoGenerateColumns="False">
        <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" />
                    <br />
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我的CS頁面-

public void show()
{
    try
    {
        dt = g1.return_dt("select id,name from tbl_data_show where en_dis='0' 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)
{
    foreach (GridViewRow row in GridMain.Rows)
    {
        CheckBox chk = sender as CheckBox;
        if (chk.Checked)
        {
            try
            {
                //Label lblid = (Label)GridMain.FindControl("Label1");
                Label lblid = new Label();
                lblid.Text = GridMain.FindControl("Label1").ToString();
                rows=g1.ExecDB("insert into tbl_data_show(en_dis) values('1') where id="+lblid.Text);
                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();
            rows=g1.ExecDB("insert into tbl_data_show(en_dis) values('0') where id="+lblid1.Text);
            if (rows > 0)
            {
                Response.Write("Rows Effected Successfull.");
            }
        }
    }
}

請指導我哪里出問題了。 我收到此異常“對象引用未設置為對象的實例。”。

好了,現在在獲得一些詳細信息之后,我可以告訴您:1)您的復選框具有autopostback = true,這將導致在每次單擊復選框時回發。 在代碼隱藏周期中,引發所有行並進行更新,因此,每次單擊任何復選框時,都將更新所有行。 2)要獲取Label.Text值,您可以嘗試執行以下操作:

CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
Label lbl = (Label)row.FindControl("Label1");
string lblTxt = lbl.Text;

或者,您可以將自定義屬性添加到您的復選框(rid =“ <%#Eval(” id“)%>”)並獲取其值,而不用隱藏標簽:

string id = ((CheckBox)sender).Attributes["rid"].ToString();
if(chkenbl.IsChecked)
{
  insert 1..
}
else
{
   insert 0..
}

暫無
暫無

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

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