如何在数据库中保存复选框选中的值

问题描述:

嗨朋友们,



我正在使用asp.net c#和SQLSERVER 2005.



on我的网页我有gridview with checkboxes,用户只能从gridview中的复选框列表中选择一个复选框。



在这个gridview中我有3列作为UserName,Password,Status



我也在gridview外提交按钮。



所以我的要求是:

----------------------



当用户选择一个复选框并点击时提交按钮。在数据库表中,它必须插入status = 1



这必须在chckbox选中并单击提交按钮时发生。



请帮助我



谢谢。

Hi friends,

am working on asp.net c# and SQLSERVER 2005.

on my webpage i have gridview with checkboxes, user can select only one checkbox from list of checkboxes inside gridview.

In this gridview i have 3 columns as UserName,Password,Status

and also i have submit button outside gridview.

So my requirement is :
----------------------

when user select one checkbox and click on submit button. In Database table it must insert status =1

This must happen when chckbox slected and submit button clicked.

Please can you help me

Thanks.

请查看获取复选框详细信息的想法来自gridview的每一行

Please see the idea to get checkbox details from each row of gridview
foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CheckBoxID");
                if (cb.Checked)
                {
                //write your code
                }
                else {
                //write your code
                }
            }


看到我不知道你的确切代码,但我告诉你如何做到这一点



foreach(GridView1.Rows中的GridViewRow行)

{

CheckBox cb =(CheckBox)row.FindControl(CheckBoxID);

HiddenField hdnid =(HiddenField)row.FindControl(hdnId);

//将您的数据库主键ID绑定在隐藏字段中,并在此处找到它

if(cb.Checked)

{

//编写代码

int ID = Convert.ToInt32(hdnid.Value) ;

string query =update tableName set status = 1其中id =+ ID;

dal.UpadateTable(查询);

}

else {

//编写代码

}

}
see i couldn't know your exact code but i am showing you how to do it

foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBoxID");
HiddenField hdnid =(HiddenField) row.FindControl("hdnId");
//Bind your database primary key id in a hidden field, and find it here
if (cb.Checked)
{
//write your code
int ID = Convert.ToInt32(hdnid.Value);
string query = "update tableName set status=1 where id="+ID;
dal.UpadateTable(query);
}
else {
//write your code
}
}