technofantasy

博客园 首页 新随笔 联系 订阅 管理
模版的代码

  1using System;
  2using System.Data;
  3using System.Web;
  4using System.Web.UI;
  5using System.Web.UI.WebControls;
  6using System.Web.SessionState;
  7
  8namespace BusinessObjects.Reference
  9{
 10    public class CustomCheckTemplate : ITemplate
 11    {
 12
 13        ListItemType templateType;
 14        string _columnName;
 15        HttpSessionState _session;
 16
 17        public string _allCheckControl = "";
 18
 19        /// <summary>
 20        /// 自定义模版的构造函数(针对数据列)
 21        /// </summary>
 22        /// <param name="type">列类型</param>
 23        /// <param name="colname">列名称</param>
 24        /// <param name="keyCols">关键指标列表</param>
 25        /// <param name="IndexID">指标项分类索引</param>

 26        public CustomCheckTemplate(ListItemType type, string colName, HttpSessionState objSession)
 27        {
 28            templateType = type;
 29            _columnName = colName;
 30            _session = objSession;
 31        }

 32
 33        public CustomCheckTemplate(ListItemType type, string colName)
 34        {
 35            templateType = type;
 36            _columnName = colName;
 37        }

 38
 39        private void TemplateControl_DataBinding(object sender,    System.EventArgs e)
 40        {
 41
 42            CheckBox check = (CheckBox)sender;
 43            
 44            
 45            DataGridItem container = (DataGridItem) check.NamingContainer;
 46
 47            object objID;
 48            string sKey = "ID";
 49            objID = DataBinder.Eval(container.DataItem, sKey);
 50
 51            check.ID = "itemCheck" + objID.ToString();
 52
 53            if(_session != null)
 54            {
 55                string sTemp = _session["OKOKTEMP"].ToString();
 56                //sTemp += objID.ToString();
 57                sTemp += check.ClientID;
 58                sTemp += "|";
 59
 60                _session["OKOKTEMP"= sTemp;
 61            }

 62            _allCheckControl += "";
 63            _allCheckControl += "|";
 64        }

 65
 66        private void OnPreRender(object sender, EventArgs e)
 67        {
 68            // Do init() work here
 69            HyperLink hl = (HyperLink)sender;
 70            hl.Attributes.Add("onclick""SelectAllCheck()");
 71        }

 72
 73        private void OnPreRender1(object sender, EventArgs e)
 74        {
 75            // Do init() work here
 76            HyperLink hl = (HyperLink)sender;
 77            hl.Attributes.Add("onclick""UnSelectAllCheck()");
 78        }

 79
 80        public void InstantiateIn(System.Web.UI.Control container)
 81        {
 82            Literal lc = new Literal();
 83            switch(templateType)
 84            {
 85                case ListItemType.Header:
 86                    lc.Text = "<B>" + _columnName + "</B>";
 87                    container.Controls.Add(lc);
 88                    break;
 89                case ListItemType.Item:
 90                    CheckBox check = new CheckBox();
 91                    container.Controls.Add(check);
 92                    check.DataBinding += new EventHandler(TemplateControl_DataBinding);
 93                    
 94                    break;
 95                case ListItemType.EditItem:
 96                    TextBox tb = new TextBox();
 97                    tb.Text = "";
 98                    container.Controls.Add(tb);
 99                    break;
100                case ListItemType.Footer:
101
102                    Literal lc1 = new Literal();
103                    Literal lc2 = new Literal();
104                    lc1.Text = "<br>";
105                    lc2.Text = "<br>";
106
107                    HyperLink hp = new HyperLink();
108                    HyperLink hp1 = new HyperLink();
109                    HyperLink hp2 = new HyperLink();
110
111                    hp.Text = "全选";
112                    hp.Style.Add("cursor","hand");
113                    hp1.Text = "不选";
114                    hp1.Style.Add("cursor","hand");
115                    hp2.Text = "删除";
116                    hp2.Style.Add("cursor","hand");
117                    hp2.Attributes.Add("onclick""DeleteSelect()");
118                    //hp1.NavigateUrl = "#";
119                
120                    hp.PreRender += new EventHandler(OnPreRender);
121                    hp1.PreRender += new EventHandler(OnPreRender1);
122                    //lc.Text = "<I>" + _columnName + "</I>";
123                    container.Controls.Add(hp);
124                    container.Controls.Add(lc1);
125                    container.Controls.Add(hp1);
126                    container.Controls.Add(lc2);
127                    container.Controls.Add(hp2);
128                    
129
130                    break;
131            }

132        }

133    }

134    /// <summary>
135    /// Summary description for CustomCheckTemplate.
136    /// </summary>

137}

138


    戏外还要先注册一个全选/不选的脚本:
 1        private void RegisterCheckScript(string IndexID)
 2        {
 3            string sTemp = Session["OKOKTEMP"].ToString();
 4            if(! sTemp.Equals(""))
 5            {
 6                sTemp = sTemp.Substring(0, sTemp.Length - 1);
 7
 8                string strJS = "<script language = \"javascript\">\n function SelectAllCheck()\n";
 9                strJS += "{\n";
10                
11                strJS += "    var allCheck = '" + sTemp + "';\n";
12                strJS += "    var arr;\n";
13                strJS += "    arr = allCheck.split('|');\n";
14                strJS += "    for(var i=0; i<arr.length;i++)\n";
15                strJS += "    {\n";
16                //strJS += "        alert(arr[i] + '.checked=true;');\n";
17                strJS += "        eval('document.Form1.' + arr[i] + '.checked=true;');\n";
18                strJS += "    }\n";
19
20                strJS += "}\n";
21
22                strJS += "function UnSelectAllCheck()\n";
23                strJS += "{\n";
24                
25                strJS += "    var allCheck = '" + sTemp + "';\n";
26                strJS += "    var arr;\n";
27                
28                strJS += "    arr = allCheck.split('|');\n";
29                strJS += "    for(var i=0; i<arr.length;i++)\n";
30                strJS += "    {\n";
31                strJS += "        eval('document.Form1.' + arr[i] + '.checked=false;');\n";
32                strJS += "    }\n";
33                
34                strJS += "}\n";
35
36                strJS += "function DeleteSelect()\n";
37                strJS += "{\n";
38                strJS += "    var allCheck = '" + sTemp + "';\n";
39                strJS += "    var arr;\n";
40                strJS += "    var sel = '';\n";
41                strJS += "    arr = allCheck.split('|');\n";
42                strJS += "    for(var i=0; i<arr.length;i++)\n";
43                strJS += "    {\n";
44                strJS += "        var x = eval('document.Form1.' + arr[i] + '.checked;');\n";
45                
46                strJS += "        if(x == true)\n";
47                strJS += "        {\n";
48                strJS += "            sel += arr[i];\n";
49                strJS += "            sel += '|';\n";
50                strJS += "        }\n";
51                strJS += "    }\n";
52                strJS += "    if(sel == '')\n";
53                strJS += "    {\n";
54                strJS += "        alert('请至少选择一项数据');\n";
55                strJS += "    }\n";
56                strJS += "    else\n";
57                strJS += "    {\n";
58                //strJS += "        alert(sel);\n";
59                strJS += "        var ret = window.showModalDialog('DeleteItem.aspx?Items=' + sel + '&IndexID=" + IndexID + 
60                                "','txtDeparment','dialogHeight: 100px; dialogWidth: 300px; dialogTop: 250px; dialogLeft: 250px; edge: Raised; center: Yes; resizable: Yes; status: No;');";
61                strJS += "        location.href='CommonList.aspx?IndexID=" + IndexID + "&GotoPage=" + (DataGrid1.CurrentPageIndex + 1).ToString() + "'\n";
62                strJS += "    }\n";
63                strJS += "}\n";
64
65                strJS += "</script>\n";
66
67                if(! this.IsClientScriptBlockRegistered("DoCheckedScript"))
68                {
69                    this.RegisterClientScriptBlock("DoCheckedScript", strJS);
70                }

71            }

72        }

  使用方法:
 1    Session["OKOKTEMP"= "";
 2    
 3    TemplateColumn tcCheck = new TemplateColumn();
 4    tcCheck.ItemStyle.Width = 50;
 5    tcCheck.HeaderTemplate = new
 6        CustomCheckTemplate(ListItemType.Header, "选择");
 7    tcCheck.ItemTemplate = new
 8        CustomCheckTemplate(ListItemType.Item, "333", Session);
 9    tcCheck.FooterTemplate = new
10            CustomCheckTemplate(ListItemType.Footer, "333");
11    
12    
13    
14    
15    DataGrid1.Columns.Add(tcCheck);

posted on 2006-06-28 16:47  陈锐  阅读(589)  评论(1)    收藏  举报