承接前一问题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不可用
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不可用
解决方案 »
- 关于动态创建类的。
- 实时曲线模块开发.高分求救!!!!!!!!!!!!!!!!
- 如果公司要求有本科学历,我是专科的,想办个假的本科可以吗?
- 请问,这样的树要怎么实现
- 利用WebRequest登录网站的问题(Post方法)
- 如何获到动态生成控件的值?结贴率100%
- webBrowser和ie一样的地址,点链接出来的页面不一样
- 重写BUTTON的BACKCOLOR属性,运行时却提示“发现不明确的匹配" 请问思归等高手....
- sumanden、icyer、hgknight。还有.net专家进来。到现在这问题还没人解决。
- 关于删除分页问题?送分
- 动态改变ImageButton的ImageUrl,不好使?
- 求asp.net自助建站系统源代码
<%@ 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>
------------------------------------
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkItem" onclick='<%# String.Format("chkItem_Click(this, {0})", Eval("CategoryID")) %>' categoryId='<%# Eval("CategoryID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
-------------------------------------
categoryId='<%# Eval("CategoryID") 有错误
chk.CheckedChanged +=new EventHandler(chk_CheckedChanged); }}
chx 没有CheckedChanged 属性(方法)
谢谢你的回复!
我用的是.net2003 ,datagrid 中的checkbox 有value属性,我再研究研究。
小项目,一个人coding,遇到问题也没人指导,只能到csdn上找大家帮忙了。
thanks a lot !
http://www.cnblogs.com/Jinglecat/archive/2006/08/31/491221.html