古董:实现跨页选择的datagrid

忘了是什么时候写的东西,最近作项目需要又翻了出来,稍微改了一下,先这么用了,呵呵
感兴趣的就拿去吧

必须要设置DataKeyField,因为保存的Key信息就是DataKeyField指定的字段的值,ValueIndex是另一个可以获取的值在dg里的列数
其他和普通的datagrid相同,需要得到选取的值就用SelectedKey和SelectedValue,它返回一个ArrayList,里面是string类型的数据
如要设置checkbox在表格中的位置,请使用CheckBoxIndex,默认是0,即第一列

  1 None.gif using  System;
  2 None.gif using  System.Web.UI;
  3 None.gif using  System.Web.UI.WebControls;
  4 None.gif using  System.Text;
  5 None.gif using  System.Data;
  6 None.gif using  System.Collections;
  7 None.gif using  System.ComponentModel;
  8 None.gif
  9 None.gif[assembly: TagPrefix( " Astrophel " , " red " )]
 10 None.gif
 11 None.gif namespace  Notus
 12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 14InBlock.gif    /// 实现跨页选择的datagrid
 15ExpandedSubBlockEnd.gif    /// </summary>

 16InBlock.gif    [DefaultProperty("CheckBoxIndex"),ToolboxData("<{0}:SuperGrid Runat=\"server\" CheckboxIndex=\"0\"></{0}:SuperGrid>")]
 17InBlock.gif    public class SuperGrid : DataGrid
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        重写 : OnInit,OnPageIndexChanged,OnLoad#region 重写 : OnInit,OnPageIndexChanged,OnLoad
 20InBlock.gif
 21InBlock.gif        //初始化DataGrid,插入自定义的带Checkbox的模板
 22InBlock.gif        protected override void OnInit(EventArgs e)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            addCheckbox();
 25InBlock.gif            base.OnInit (e);
 26ExpandedSubBlockEnd.gif        }

 27InBlock.gif
 28InBlock.gif        //设定模板列中Checkbox的Checked属性的值
 29InBlock.gif        public override void DataBind()
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            
 32InBlock.gif            base.DataBind ();
 33InBlock.gif            this.setGrid(this);
 34ExpandedSubBlockEnd.gif        }

 35InBlock.gif
 36InBlock.gif
 37InBlock.gif        //保存选择的值
 38InBlock.gif        //这里保存的实际上是上一页选择的值,CurrentPageIndex的值为前一页值
 39InBlock.gif        protected override void OnLoad(EventArgs e)
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 41InBlock.gif            this.saveMsg(this);
 42InBlock.gif            base.OnLoad (e);
 43ExpandedSubBlockEnd.gif        }

 44InBlock.gif
 45ExpandedSubBlockEnd.gif        #endregion

 46InBlock.gif
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        属性 : CheckBoxIndex,SelectedMessage
#region 属性 : CheckBoxIndex,SelectedMessage
 48InBlock.gif
 49InBlock.gif        //CheckBox插入列的索引
 50InBlock.gif        private int checkBoxIndex=0;
 51InBlock.gif
 52InBlock.gif        [DefaultValue(0),Description("CheckBox插入列的索引")]
 53InBlock.gif        public int CheckBoxIndex
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gifthis.checkBoxIndex=value; }
 56ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return this.checkBoxIndex; }
 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif
 59InBlock.gif        //选择的值
 60InBlock.gif        [Browsable(false)]
 61InBlock.gif        public ArrayList SelectedValue
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn getMsg(); }
 64InBlock.gif
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif
 67InBlock.gif        //选择的键
 68InBlock.gif        [Browsable(false)]
 69InBlock.gif        public ArrayList SelectedKey
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn getKey(); }
 72InBlock.gif
 73ExpandedSubBlockEnd.gif        }

 74InBlock.gif
 75InBlock.gif        public string Result
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            get
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                System.Text.StringBuilder sb=new System.Text.StringBuilder();
 80InBlock.gif                ArrayList msg=this.SelectedValue;
 81InBlock.gif                ArrayList key=this.SelectedKey;
 82InBlock.gif                for(int i=0;i<msg.Count;i++)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 84InBlock.gif                    sb.Append(key[i].ToString());
 85InBlock.gif                    sb.Append("");
 86InBlock.gif                    sb.Append(msg[i].ToString());
 87InBlock.gif                    sb.Append("<br>");
 88ExpandedSubBlockEnd.gif                }

 89InBlock.gif                return sb.ToString();
 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif        private int vi;
 94InBlock.gif
 95InBlock.gif        [DefaultValue(1),Description("要获取的值的存放索引")]
 96InBlock.gif        public int ValueIndex
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{this.vi=value;}
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return this.vi;}
100ExpandedSubBlockEnd.gif        }

101InBlock.gif
102ExpandedSubBlockEnd.gif        #endregion

103InBlock.gif
104ExpandedSubBlockStart.gifContractedSubBlock.gif        加入一有CheckBox的模板 : addCheckbox#region 加入一有CheckBox的模板 : addCheckbox
105InBlock.gif        
106ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
107InBlock.gif        /// 加入一有ckeckbox的模板
108ExpandedSubBlockEnd.gif        /// </summary>

109InBlock.gif        private void addCheckbox()
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
111InBlock.gif            TemplateColumn tc=new TemplateColumn();
112InBlock.gif            tc.ItemTemplate=new CheckboxTemplate();
113InBlock.gif            this.Columns.AddAt(checkBoxIndex,tc);
114ExpandedSubBlockEnd.gif        }

115InBlock.gif
116ExpandedSubBlockEnd.gif        #endregion

117InBlock.gif
118ExpandedSubBlockStart.gifContractedSubBlock.gif        具体操作 : saveMsg,setGrid#region 具体操作 : saveMsg,setGrid
119InBlock.gif        
120ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
121InBlock.gif        /// 选出Session中该页的被选记录的值,根据这些值来决定CheckBox的Checked属性的值
122ExpandedSubBlockEnd.gif        /// </summary>

123InBlock.gif        private void setGrid(DataGrid dg)
124ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
125InBlock.gif            this.CheckSession();
126InBlock.gif            
127InBlock.gif            DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
128InBlock.gif            DataRow[] rows=dt.Select("pageIndex="+dg.CurrentPageIndex);
129InBlock.gif
130InBlock.gif            foreach(DataRow row in rows)
131ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
132InBlock.gif                CheckBox cb=(CheckBox)dg.Items[(int)row["itemIndex"]].Cells[checkBoxIndex].FindControl("cb");
133InBlock.gif                cb.Checked=true;
134ExpandedSubBlockEnd.gif            }

135InBlock.gif
136InBlock.gif            dt.AcceptChanges();
137InBlock.gif            
138ExpandedSubBlockEnd.gif        }

139InBlock.gif
140ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
141InBlock.gif        /// 在Session中保存该页选中的记录
142InBlock.gif        /// </summary>
143InBlock.gif        /// 考虑到用户可能回来回翻页并修改选项,这个算法是我所能想出来的最好一个
144ExpandedSubBlockEnd.gif        /// 先把该页以前的记录删除,然后再插入新选择的记录

145InBlock.gif        private void saveMsg(DataGrid dg)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            this.CheckSession();
148InBlock.gif
149InBlock.gif            DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
150InBlock.gif            DataRow[] rows=dt.Select("pageIndex="+dg.CurrentPageIndex);
151InBlock.gif
152InBlock.gif            foreach(DataRow row in rows)
153ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
154InBlock.gif                row.Delete();
155ExpandedSubBlockEnd.gif            }

156InBlock.gif            foreach(DataGridItem item in dg.Items)
157ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
158InBlock.gif                CheckBox cb=(CheckBox)item.Cells[checkBoxIndex].FindControl("cb");
159InBlock.gif                if(cb.Checked)
160ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
161InBlock.gif                    int itemIndex=item.ItemIndex;
162ExpandedSubBlockStart.gifContractedSubBlock.gif                    dt.Rows.Add(new object[]dot.gif{dg.CurrentPageIndex,itemIndex,dg.DataKeys[itemIndex],item.Cells[vi].Text});
163ExpandedSubBlockEnd.gif                }

164ExpandedSubBlockEnd.gif            }

165InBlock.gif
166InBlock.gif            dt.AcceptChanges();
167ExpandedSubBlockEnd.gif        }

168InBlock.gif        
169ExpandedSubBlockEnd.gif        #endregion

170InBlock.gif
171ExpandedSubBlockStart.gifContractedSubBlock.gif        得到选择的值 : getMsg#region 得到选择的值 : getMsg
172InBlock.gif
173ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
174InBlock.gif        /// 获得session中现存的值
175ExpandedSubBlockEnd.gif        /// </summary>

176InBlock.gif        private ArrayList getMsg()
177ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
178InBlock.gif            this.CheckSession();
179InBlock.gif            this.saveMsg(this);
180InBlock.gif            ArrayList msg=new ArrayList();
181InBlock.gif            DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
182InBlock.gif            foreach(DataRow row in dt.Rows)
183ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
184InBlock.gif                msg.Add(row[3].ToString());
185ExpandedSubBlockEnd.gif            }

186InBlock.gif            
187InBlock.gif            return msg;
188ExpandedSubBlockEnd.gif        }

189InBlock.gif
190ExpandedSubBlockEnd.gif        #endregion

191InBlock.gif
192ExpandedSubBlockStart.gifContractedSubBlock.gif        得到选择的键 : getKey#region 得到选择的键 : getKey
193InBlock.gif
194ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
195InBlock.gif        /// 获得session中现存的值
196ExpandedSubBlockEnd.gif        /// </summary>

197InBlock.gif        private ArrayList getKey()
198ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
199InBlock.gif            this.CheckSession();
200InBlock.gif            this.saveMsg(this);
201InBlock.gif            ArrayList msg=new ArrayList();
202InBlock.gif            DataTable dt=(DataTable)Context.Session["SuperGridMsg"];
203InBlock.gif            foreach(DataRow row in dt.Rows)
204ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
205InBlock.gif                msg.Add(row[2].ToString());
206ExpandedSubBlockEnd.gif            }

207InBlock.gif            
208InBlock.gif            return msg;
209ExpandedSubBlockEnd.gif        }

210InBlock.gif
211ExpandedSubBlockEnd.gif        #endregion

212InBlock.gif
213ExpandedSubBlockStart.gifContractedSubBlock.gif        清除session中的记录#region 清除session中的记录
214InBlock.gif
215InBlock.gif        public void ClearSession()
216ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
217InBlock.gif            Context.Session["SuperGridMsg"]=null;
218InBlock.gif            Context.Session.Remove("SuperGridMsg");
219ExpandedSubBlockEnd.gif        }

220ExpandedSubBlockEnd.gif        #endregion

221InBlock.gif
222ExpandedSubBlockStart.gifContractedSubBlock.gif        辅助部分 : CheckboxTemplate,CheckSession#region 辅助部分 : CheckboxTemplate,CheckSession
223InBlock.gif
224ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
225InBlock.gif        /// 带有CheckBox的模板类
226ExpandedSubBlockEnd.gif        /// </summary>

227InBlock.gif        public class CheckboxTemplate : ITemplate
228ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
229InBlock.gif            public void InstantiateIn(Control container)
230ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
231InBlock.gif                CheckBox cb=new CheckBox();
232InBlock.gif                cb.ID="cb";
233InBlock.gif                container.Controls.Add(cb);
234ExpandedSubBlockEnd.gif            }

235ExpandedSubBlockEnd.gif        }

236InBlock.gif
237ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
238InBlock.gif        /// 检查session是否存在
239ExpandedSubBlockEnd.gif        /// </summary>

240InBlock.gif        private void CheckSession()
241ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
242InBlock.gif            if(Context.Session["SuperGridMsg"]==null)
243ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
244InBlock.gif                DataTable dt=new DataTable();
245InBlock.gif                dt.Columns.Add("pageIndex",typeof(int));
246InBlock.gif                dt.Columns.Add("itemIndex",typeof(int));
247InBlock.gif                dt.Columns.Add("ID");
248InBlock.gif                dt.Columns.Add("Value");
249InBlock.gif                Context.Session["SuperGridMsg"]=dt;
250ExpandedSubBlockEnd.gif            }

251ExpandedSubBlockEnd.gif        }

252ExpandedSubBlockEnd.gif        #endregion

253InBlock.gif
254ExpandedSubBlockEnd.gif    }
255ExpandedBlockEnd.gif}

1 None.gif < notus:supergrid id = " list "  runat = " server "  AutoGenerateColumns = " False "  DataKeyField = " NengliID "  CheckboxIndex = " 0 "
2 None.gif                ValueIndex = " 1 " >
3 None.gif                 < Columns >
4 None.gif                     < asp:BoundColumn DataField = " MingZi "  HeaderText = " 名称 " ></ asp:BoundColumn >
5 None.gif                 </ Columns >
6 None.gif             </ notus:supergrid >


 1 ExpandedBlockStart.gif ContractedBlock.gif 绑定数据 #region 绑定数据
 2InBlock.gif        private void bind(int pageIndex)
 3ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 4InBlock.gif            BLL.AdminZhiYe az=new CZhiYe.BLL.AdminZhiYe();
 5InBlock.gif            int count=0;
 6InBlock.gif            this.list.CurrentPageIndex=pageIndex;
 7InBlock.gif            this.list.DataSource=(az.GetNengLiSplit(pageIndex,ref count)).GetAll();
 8InBlock.gif            this.list.DataBind();
 9InBlock.gif            pages.RecordCount=count;
10InBlock.gif            pages.CurrentPageIndex=pageIndex;
11InBlock.gif            pages.PageSize=Params.Config.PageSize;
12InBlock.gif
13InBlock.gif            this.val.Text=this.list.Result;
14ExpandedSubBlockEnd.gif        }

15ExpandedBlockEnd.gif        #endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值