首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

datagrid 的一行里有两个checkbox,怎么得到checkbox是否选中

2012-01-06 
datagrid 的一行里有两个checkbox,如何得到checkbox是否选中?datagrid的一行里有两个checkbox,如何得到che

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.NewRow();
row[0] = 48;


row[1] = "Chocolade ";
row[2] = 3;
tbl.Rows.Add(row);

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

return tbl;
}

#endregion


</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> DataGrid RadioButton </title>

</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataGrid ID= "grdPro " DataKeyField= "ProductID " runat= "server ">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
选项1: <input type= "radio " id= "rbtnProduct " name= "rbtnProduct " value= ' <%# Container.ItemIndex + "$Flag1 " %> ' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
选项2: <input type= "radio " id= "rbtnProduct " name= "rbtnProduct " value= ' <%# Container.ItemIndex + "$Flag2 " %> ' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button ID= "Button1 " runat= "server " Text= "Button " OnClick= "Button1_Click " />
</div>
</form>
</body>
</html>

[解决办法]
跟一个差不多吧只是ID不同而已啊
[解决办法]
无非就是findcontrol定位控件嘛
[解决办法]

for(int i = 0; i < this.dataGrid.Items.Count; i++)
{
CheckBox ckb =new CheckBox();
ckb1 = (CheckBox)(this.dataGrid.Items[i].FindControl( "CheckBox1的ID "));
ckb2= (CheckBox)(this.dataGrid.Items[i].FindControl( "CheckBox2的ID "));
if(ckb1.Check==True)
{
Response.wtrite(this.DataGrid.DataKeys[i].ToString());
}
......
}

热点排行