朱金灿
前言:最近使用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> </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号。
分享到:
相关推荐
DataGrid自定义列标题 DataGrid自定义列标题 DataGrid自定义列标题 DataGrid自定义列标题 DataGrid自定义列标题
项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选、全选。 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellControlTemplate,Binding, OnPropertyChanged等。
WPF中DataGrid里面的Checkbox实现单选关键代码, 文档里面的代码复制粘贴即可实现!本人也是项目需要写的一个功能11行代码实现此功能! 希望可以帮助到你们!
datagrid 动态添加模板列 asp.net 模板列继承ITemplate 绑定字段名称
WPF中的DataGrid合并单元格,模板列内嵌套DataGrid,非真正合并
DataGrid模板列自定义控件使用,依赖属性的绑定
绝对好东西,datagrid 标题栏及 datagridcolumn 添加 checkbox
基于siverlight动态生成DataGrid模板列源码例子
实现了向dataGrid中添加checkBox的功能,可以支持多选,单选,全选。
本实例是在datagrid里插入多列Checkbox的应用,不存在拖动下拉条混乱问题,里面代码很简单,但希望可以帮到你。
wpf 下DataGrid表头包含checkbox,实现多选功能,wpf 下DataGrid表头包含checkbox,实现多选功能
其中包含一个三种状态的checkbox,DataGrid代码中引用了 带多选框的DataGrid(AdvancedDataGrid) 表头有全选CheckBox,三种状态:全选、部分选择、无选择 如果改变目录结构,需要在CustomCheckBox.mxml中更改inner....
Flex的DataGrid中使用CheckBox. 大家共同研究
其中最常见的方法是在web forms设计器中增加,通过在控件工具箱中拖访DataGrid控件到web设计页面,然后在属性生成器中增加Columns列;还有一种方式就是在 HTML视图模式下更改HTML代码的方式增加Columns列。本文中将向...
WPF DataGrid模板选择器,可以根据不同类型选择不同模板
使用MVVM模式对WPF中的DataGrid控件动态添加列,并实现了编辑模式。
NULL 博文链接:https://taomujian.iteye.com/blog/294748
在Flex的DataGrid中使用CheckBox 欢迎分享
可以通过简单的一个标签实现一个表的checkbox功能,当然我才学了不到一个星期,可以在原来的基础上进行扩展,希望交流下我的邮箱是llaysz@163.com,如果大家把它变强大了记得给我发一份谢谢了
在easyui 的datagrid编辑状态下实时更新其他处于编辑或者没在编辑下的列值,并在结束编辑后得到的row的数据为更改后的数据。