承接前一问题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不可用

解决方案 »

  1.   

    so easy, just waiting
      

  2.   

    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>
      

  3.   

    给段参考代码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;    }}
      

  4.   

    要是代码可以跑起来的话,多分点给 高歌 哈 thx
      

  5.   

    简单的说就是ItemDataBound中根据条件控制控件属性
      

  6.   

    to Jinglecat:
    ------------------------------------
            <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") 有错误
      

  7.   

    to xocom(福大) :
    chk.CheckedChanged +=new EventHandler(chk_CheckedChanged); }}
    chx 没有CheckedChanged 属性(方法)
      

  8.   

    categoryId='<%# Eval("CategoryID") 有错误===========什么错误?我是本地测试通过的啊,看看是不是贴错了,完整是 categoryId='<%# Eval("CategoryID") %>' 啊事实上由于 CheckBox 不支持 value 属性,所以这里我采取了点技巧,呵呵
      

  9.   

    categoryId='<%# Eval("CategoryID") 有错误,你看看你的vs,是不是2003的??
      

  10.   

    categoryId='<%# Eval("CategoryID") %>'》》要是 1.x 这个绑定语法 LZ 应该会自己修改过来的撒categoryId='<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>'
      

  11.   

    to Jinglecat: 
    谢谢你的回复!
    我用的是.net2003 ,datagrid 中的checkbox 有value属性,我再研究研究。
    小项目,一个人coding,遇到问题也没人指导,只能到csdn上找大家帮忙了。
    thanks a lot !
      

  12.   

    datagrid 中的checkbox 有value属性=======不会哦,记得 1.x 中 asp:checkbox 不支持此属性的, 我自己还些写了个 XCheckBox 来支持 value带Value属性的扩展CheckBox控件 
    http://www.cnblogs.com/Jinglecat/archive/2006/08/31/491221.html