`
ladymaidu
  • 浏览: 680941 次
文章分类
社区版块
存档分类
最新评论

对利用Session纪录datagrid模板列中CheckBox的状态的一点改进

 
阅读更多
朱金灿
前言:最近使用asp.net做开发,有了一些心得,特写出来,望能起一个抛砖引玉的作用。
在DataGrid 的模板列中加入CheckBox ,如果对DataGrid设置分页,前一页已经选中的CheckBox在回到原页时,CheckBox的状态会变为初始状态。
如果想保存checkbox的状态,则可以用Session保存。我从网上下载了一篇文章是这样做的:
1、首先,建立DataGrid.
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 12px; POSITION: absolute; TOP: 88px" runat="server" AllowPaging="True" Width="416px" Height="248px">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<ItemStyle Font-Size="12px" Font-Names="宋体"></ItemStyle>
<HeaderStyle Font-Size="12px" Font-Names="宋体" BackColor="#00CCFF"></HeaderStyle>
<FooterStyle HorizontalAlign="Center"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:checkbox id="CheckBox1" runat="server"></asp:checkbox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
2、在Global.asax文件中,Session_Start事件中建立Session
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
'
在会话启动时激发
Dim checks As Boolean() = New Boolean(10000) {}
Dim i As Integer
For i = 0 To 10000
checks(i) = False
Next
Session.Add("CheckBoxchecks", checks)
End Sub
3.在DataGrid的PageIndexChanged事件中:
PrivateSub DataGrid1_PageIndexChanged(ByVal source AsObject, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
Dimcount AsInteger
Dim cnn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/UserLog.mdb")
Dim da AsNew OleDb.OleDbDataAdapter("select * from mm", cnn)
da.Fill(dst)
count = dst.Tables(0).Rows.Count 'DataGrid中的记录总数;
Dim check AsBoolean()
check = Me.Session("checkboxchecks")'check()是记录checkbox状态的数组,用session保存。
Dim j AsInteger
For j = 0 To DataGrid1.PageSize - 1
Dim Che As CheckBox = DataGrid1.Items(j).Cells(0).FindControl("CheckBox1")
IfNot Che IsNothingThen
If Che.Checked = TrueThen
check(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + j) = True
Else
check(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + j) = False
EndIf
EndIf
Next
DataGrid1.CurrentPageIndex = e.NewPageIndex
Dim ds AsNew DataSet()
da.Fill(ds, "a")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
Dim i AsInteger
For i = 0 To DataGrid1.PageSize - 1
Dim Cx2 As CheckBox = DataGrid1.Items(i).Cells(0).FindControl("CheckBox1")
If check(DataGrid1.CurrentPageIndex * DataGrid1.PageSize + i) = TrueThen
Cx2.Checked = True
Else
Cx2.Checked = False
EndIf
Next
EndSub
这样就可以利用Session实现checkbox翻页后的状态保存问题.
实际上我对该文的看法是提供了一个好思路,但是具体实现无疑显得拙劣了一点,具体表现在:一.文中首先定义了一个有一万个元素的数组用来保存checkbox的状态, Dim checks As Boolean() = New Boolean(10000) {},这样一来当记录数大于10000时显然是不够用的,而小于10000条时又浪费了;二是文中程序对选中和未选中都进行记录,这有点多余,其实我们只要记录选中的id号就行了。鉴于此,我和同事对此进行了一点改进(这里采用C#实现)。
1、首先,建立DataGrid,其中checkbox的id为ChkSelected。
<asp:datagrid id="DataList" runat="server" Width="542px" PageSize="4" DataKeyField="book_id" AutoGenerateColumns="False"BorderStyle="None" CellPadding="3" BorderWidth="1px" BackColor="White" BorderColor="#E6E7E8"Height="0px" AllowPaging="True" Font-Size="12px" Font-Names="Arial"><SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<AlternatingItemStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#EBF8FE"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center" ForeColor="Black" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="12px" Font-Names="Arial" Font-Bold="True" HorizontalAlign="Center" ForeColor="White"BackColor="#FF9900"></HeaderStyle><FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle><Columns>
<asp:BoundColumn DataField="book_name" HeaderText="介质名称"></asp:BoundColumn>
<asp:BoundColumn DataField="book_keyword" HeaderText="关键词"></asp:BoundColumn><asp:BoundColumn DataField="book_depoisit" HeaderText="保存位置"></asp:BoundColumn><asp:BoundColumn DataField="book_inlibdate" HeaderText="入库时间" DataFormatString="{0:yyyy-M-d }"></asp:BoundColumn><asp:BoundColumn DataField="fieldvalue" HeaderText="处理状态"></asp:BoundColumn><asp:TemplateColumn HeaderText="是否选中"><HeaderTemplate><FONT></FONT></HeaderTemplate><ItemTemplate><FONT>&nbsp;</FONT><asp:CheckBox ID="ChkSelected" runat="server" Text="" Checked="False"></asp:CheckBox></ItemTemplate></asp:TemplateColumn></Columns><PagerStyle Font-Size="12px" Font-Names="Arial" HorizontalAlign="Left" ForeColor="Blue" BackColor="White"
Mode="NumericPages"></PagerStyle>
</asp:datagrid></FONT></P>
2. 新建一个私有函数用于保存当前选择的数据项
private void SaveSelecedSession(){
// 从session中取出ArrayList
ArrayList al = (ArrayList)this.Session["BookManage_CheckBoxchecks"];
// 假如ArrayList不存在,就新建一个
if (al == null)
al = new ArrayList();
for(int n = 0;n<DataList.Items.Count;n++)
{
CheckBox cb = (CheckBox)DataList.Items[n].Cells[3].FindControl("ChkSelected");
if(cb!=null){
// 取出记录的id号
string id = DataList.DataKeys[DataList.Items[n].ItemIndex].ToString();
// 判断id是否存在ArrayList中
bool con =al.Contains(id);
if (cb.Checked){
// 假如被选中同时又不存在ArrayList中,将id号加进ArrayList.
if (!con)
al.Add(id);
}
else
{
//假如被选中同时又存在ArrayList中,将该id号从ArrayList中删除,此举用于确保//ArrayList中的id号是唯一的。
if (con)
{
al.Remove(id);
}
}
}
}
//保存结果到Session中
this.Session["BookManage_CheckBoxchecks"] = al;
}
3.在DataGrid的PageIndexChanged事件响应函数中调用SaveSelectedSession函数,具体如下:
private void DataList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{//保存当前选择的数据项
SaveSelecedSession();
//邦定数据表
DataList.CurrentPageIndex = e.NewPageIndex;
string ExecuteSql = "select a.book_id,a.book_name,a.book_keyword,a.book_depoisit,a.book_inlibdate,b.fieldvalue from intel.cs_bookinfo a,intel.cs_dictionary b where a.book_status!=4 and a.book_status=b.fieldid and b.tablename='CS_BookInfo' and b.fieldname = 'Book_Status'";
RefreshData(ExecuteSql);
ArrayList al = (ArrayList)this.Session["BookManage_CheckBoxchecks"];
if (al == null)
al = new ArrayList();
//根据原来保存的选择项id号对checkbox进行初始化
for(int n = 0;n<DataList.Items.Count;n++)
{
CheckBox cb = (CheckBox)DataList.Items[n].Cells[3].FindControl("ChkSelected");
if(cb!=null)
{
string id= DataList.DataKeys[DataList.Items[n].ItemIndex].ToString();
bool con =al.Contains(id);
if (!con)
cb.Checked = false;
else
cb.Checked = true;
}
}
}
这样做,就可以克服上文的程序的缺点:应对不确定的记录数(因为ArrayList是动态增加的),同时又只保存了选择中记录的id号。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics