[英]Using a checkbox input in Gridview and inserting is value in to SQL DB
[英]Inserting data to GridView using CheckBox
我有一個GridView,並在其所有單元格中放置了復選框。 我要執行以下操作:如果該復選框由用戶選中,則表示是,它將存儲在數據庫中;否則,如果該復選框未被用戶選中,則表示否,並且不需要將任何內容發布到數據庫。
我知道我現在需要標識每個選中的復選框,並知道此選中的復選框位於哪個單元格下面。
關於如何做到這一點的任何想法? 有人可以給我做這件事的基本代碼嗎?
我使用類似的東西:
foreach (GridViewRow row in gvYourGridView.Rows)
{
CheckBox ck = ((CheckBox)row.FindControl("YourCheckBoxName"));
if (ck.Checked)
{
//If checked is true, update database.
}
}
int counter = 0;
foreach (GridViewRow rowitem in gvYourGridView.Rows)
{
if (((CheckBox)rowitem.Cells[0].FindControl("chk")).Checked == true)//i consider that the check box is in the first column index ---> 0
{
counter++;
}
}
/////////////////////////////////////////////////////////////
if(counter == 0) //no checks
{
//show some message box to clarify that no row has been selected.
}
/////////////////////////////////////////////////////////////
if (counter == 1) //one check
{
//Do something
}
/////////////////////////////////////////////////////////////
if (counter > 1) //more than one check
{
//Do something
}
gvYourGridView.DataBind();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.