日期:2014-05-18  浏览次数:20493 次

datagrid 的一行里有两个checkbox,如何得到checkbox是否选中?
datagrid   的一行里有两个checkbox,如何得到checkbox是否选中?

在后台如何得到checkbox是否选中?
根据选中状态写到以应的数据库字段里

------解决方案--------------------
for(int i = 0; i < this.dataGrid.Items.Count; i++)
{
CheckBox ckb =new CheckBox();
ckb = (CheckBox)(this.dataGrid.Items[i].FindControl( "CheckBox的ID "));
if(ckb.Check==True)
{
Response.wtrite(this.DataGrid.DataKeys[i].ToString());
}
}

------解决方案--------------------
<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadProductData();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(Request.Form[ "rbtnProduct "])) throw new Exception( "请选择一项 ");

string[] selectedRadioButtonValueArray = Request.Form[ "rbtnProduct "].Split( '$ '); // 获取被选中 radio 的值, rbtnProduct 为 radio 的客户端 name 属性值
int selectedItemIndex = int.Parse(selectedRadioButtonValueArray[0]); // 选择项索引
DataGridItem selectedGridItem = grdPro.Items[selectedItemIndex]; // 获取 DataGrid 的选择项
Response.Write(selectedGridItem.Cells[3].Text); // test
string whichRadioInItemFlag = selectedRadioButtonValueArray[1];
Response.Write(whichRadioInItemFlag);
if (whichRadioInItemFlag == "Flag1 ") {
// ...
}
else if (whichRadioInItemFlag == "Flag2 ") {
// ...
}
// more ...
//else {
//}
}

void LoadProductData()
{
grdPro.DataSource = CreateProductTable();
grdPro.DataBind();
}

#region sample data

static DataTable CreateProductTable()
{
DataTable tbl = new DataTable( "Products ");

tbl.Columns.Add( "ProductID ", typeof(int));
tbl.Columns.Add( "ProductName ", typeof(string));
tbl.Columns.Add( "CategoryID ", typeof(int));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chai ";
row[2] = 1;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 2;
row[1] = "Chang ";
row[2] = 1;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 3;
row[1] = "Aniseed Syrup ";
row[2] = 2;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 4;
row[1] = "Chef Anton 's Cajun Seasoning ";
row[2] = 2;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 5;
row[1] = "Chef Anton 's Gumbo Mix ";
row[2] = 2;
tbl.Rows.Add(row);

row = tbl.NewRow();
row[0] = 47;
row[1] = "Zaanse koeken ";
row[2] = 3;
tbl.Rows.Add(row);

row = tbl.NewR