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

datagrid 中checkbox控制的有关问题

2012-01-30 
datagrid 中checkbox控制的问题承接前一问题http://community.csdn.net/Expert/topic/5652/5652860.xml?te

datagrid 中checkbox控制的问题
承接前一问题http://community.csdn.net/Expert/topic/5652/5652860.xml?temp=.2189752
datagrid中最后一列是checkbox
                 buttonA(datagrid之外)
--------------------------------------------
A1:Taiwan
------------------------------------
product_CD0,item_CD,product_name,....□
--------------------------------------------
A2:Hongkong
------------------------------------
product_CD1,item_CD,product_name,....□
product_CD2,item_CD,product_name,....□
product_CD3,item_CD,product_name,....□

--------------------------------------------
A3:China
------------------------------------
product_CD5,item_CD,product_name,....□
--------------------------------------------

1,每个区域中只能有一个checkbox可以使用
2,datagrid中有checkbox被选中的时候,buttonA可用
一个都没有的时候,buttonA不可用


[解决办法]
just try

<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>

<%--
http://community.csdn.net/Expert/TopicView3.asp?id=5652860
--%>

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

<script runat= "server ">

private int lastCategoryId = -1; // 私有字段,当前绑定 DataGrid 行的上一行的 CategoryId

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

protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;
if (drv != null) {
int currentCategoryId = (int)drv[ "CategoryID "];
// 比较当前行 与 上一行 的 CategoryId
if (lastCategoryId != currentCategoryId) {
//
DataGridItem itemCategory = new DataGridItem(-1, -1, ListItemType.Item);
TableCell emptyCell = new TableCell();
emptyCell.Text = GetCategoryName(currentCategoryId);
emptyCell.ColumnSpan = DataGrid1.Columns.Count; // 合并列
itemCategory.Cells.Add(emptyCell);
// 在当前行之前插入一行
DataGrid1.Controls[0].Controls.AddAt(DataGrid1.Controls[0].Controls.Count - 1, itemCategory);
//
lastCategoryId = currentCategoryId;
}
}
}

string GetCategoryName(int categoryId)
{
switch (categoryId) {
case 1 :
return "A1:Taiwan ";
case 2:
return "A2:Hongkong ";
case 3:
return "A3:RPC ";
default:
return "unknown ";
}
}

void LoadProductData()
{
DataTable dt = CreateProductTable();
DataView dv = dt.DefaultView;
dv.Sort = "CategoryID, ProductID ";
DataGrid1.DataSource = dv;
DataGrid1.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));
tbl.Columns.Add( "HasPic ", typeof(bool));


tbl.Columns.Add( "Reviewed ", typeof(bool));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chai ";
row[2] = 1;
row[3] = true;
row[4] = false;
tbl.Rows.Add(row);

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

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

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

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

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

row = tbl.NewRow();
row[0] = 48;
row[1] = "Chocolade ";
row[2] = 3;
row[3] = false;
row[4] = false;
tbl.Rows.Add(row);

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

return tbl;
}

#endregion

</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> CSDN_DataGridMainHeaderRow </title>
<script type= "text/javascript ">
function chkItem_Click(sender, categoryId)
{
//debugger;
//if(!sender.checked) return;
var chkState = sender.checked;
var grd = document.getElementById( ' <% =DataGrid1.ClientID %> ');
var chkList = document.getElementsByTagName( "input ");
var chk;
var canSubmit = chkState;
for(var i = 0; i < chkList.length; i++){
chk = chkList[i];
if(chk.type == "checkbox " && chk.id.indexOf( "chkItem ") > = 0) {
if(chkState && chk.parentNode.categoryId == categoryId.toString()) {
chk.checked = false;
}
if(chk.checked) canSubmit = true;
}
}
sender.checked = chkState;
document.getElementById( ' <% =Button1.ClientID %> ').disabled = !canSubmit;
}
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:Button ID= "Button1 " runat= "server " Enabled= "false " Text= "Sumbit " />
<asp:DataGrid ID= "DataGrid1 " runat= "server " AutoGenerateColumns= "false " DataKeyField= "ProductID " OnItemDataBound= "DataGrid1_ItemDataBound ">
<Columns>
<asp:BoundColumn DataField= "ProductID " HeaderText= "ProductID "> </asp:BoundColumn>


<asp:BoundColumn DataField= "ProductName " HeaderText= "ProductName " > </asp:BoundColumn>
<asp:BoundColumn DataField= "CategoryID " HeaderText= "CategoryID " > </asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID= "chkItem " onclick= ' <%# String.Format( "chkItem_Click(this, {0}) ", Eval( "CategoryID ")) %> ' categoryId= ' <%# Eval( "CategoryID ") %> ' runat= "server " />

</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>

[解决办法]
给段参考代码

DataGridItem的时候给它添加事件代码如下:

private void grdServer_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {

if(e.Item.ItemType == ListItemType.Header){

CheckBox chk = (CheckBox)e.Item.FindControl( "chkAllServer ");

// 给页眉上的CheckBox添加出发事件

chk.CheckedChanged +=new EventHandler(chk_CheckedChanged); }}

事件处理程序如下所示:

// 得到指定DataGrid的题头的CheckBox对象

private CheckBox GetHeaderCheckBox(DataGrid grd){ CheckBox chk = null;

foreach (DataGridItem i in grd.Controls[0].Controls){

if(i.ItemType == ListItemType.Header){

chk = (CheckBox)i.FindControl( "chkAllServer ");

break;

}

}

return chk;

}

private void chk_CheckedChanged(object sender, System.EventArgs e) {

CheckBox chk = this.GetHeaderCheckBox(this.grdServer);

foreach (DataGridItem i in this.grdServer.Items){

CheckBox inChk = (CheckBox)i.FindControl( "chkDelServer ");

inChk.Checked = chk.Checked;

}

}


[解决办法]
来晚..
[解决办法]
简单的说就是ItemDataBound中根据条件控制控件属性
[解决办法]
帮顶

接点分
[解决办法]
categoryId= ' <%# Eval( "CategoryID ") 有错误,你看看你的vs,是不是2003的??
[解决办法]
帮定,
接分

热点排行