繁体   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