在ASP.NET跨页多选

本文介绍怎样在ASP.NET中实现多页面选择的问题。其详细思路非常easy:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时。检查保存的值,再在DataGrid中进行选中显示。

以下时完整的代码和样例:

  查看样例

  SelectMultiPages.aspx

<%@ Page EnableViewState="true" CodeBehind="SelectMultiPages.aspx.cs" Language="c#" 

AutoEventWireup="false" Inherits="eMeng.Exam.SelectMultiPages" %> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

<HTML> 

<HEAD> 

<title>跨页面实现多选</title> 

<META http-equiv="content-type" content="text/html; charset=gb2312"> 

<style> 

* {FONT-SIZE:12PX} 

#Status {text-align:left} 

</style> 

<script language="JAVASCRIPT"> 

function AddRemoveValues(oChk) { 

//在处理这个地方须要注意的是:你保存的值应该具有唯一性。这样才干不会替换错误的项。

if(oChk.checked) 

SelectMultiPage.HdnSelectedValues.value += "," + oChk.value; 

else 

SelectMultiPage.HdnSelectedValues.value = SelectMultiPage.HdnSelectedValues.value.replace("," + oChk.value,""); 



</script> 

</HEAD> 

<BODY> 

<form id="SelectMultiPage" runat="server"> 

<asp:datagrid id="DataGrid1" HorizontalAlign="Center" AutoGenerateColumns="False" Width="600px" 

AllowPaging="True" runat="server"> 

<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle> 

<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle> 

<PagerStyle HorizontalAlign="Right" Mode="NumericPages" Visible="True"></PagerStyle> 

<Columns> 

<asp:TemplateColumn HeaderText="选择"> 

<ItemTemplate> 

<input type="checkbox" runat="server" id="chkSelect" onclick="AddRemoveValues(this)" 

value='<%#DataBinder.Eval(Container.DataItem,"Title")%>'/> 

</ItemTemplate> 

</asp:TemplateColumn> 

<asp:TemplateColumn HeaderText="文章标题"> 

<ItemTemplate> 

<asp:Literal Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" ID="TitleShow"/> 

</ItemTemplate> 

</asp:TemplateColumn> 

<asp:TemplateColumn HeaderText="公布时间"> 

<ItemTemplate> 

<asp:Literal Text='<%# DataBinder.Eval(Container.DataItem, "CreateDate").ToString() %>' runat="server"/> 

</ItemTemplate> 

</asp:TemplateColumn> 

</Columns> 

</asp:datagrid> 

<div align=center> 

<asp:button id="Button1" runat="server" Text="得到所选的值"></asp:button> 

<div id="Status"> 

<asp:label id="Label1" runat="server"></asp:label> 

</div> 

<INPUT id="HdnSelectedValues" type="hidden" name="HdnSelectedValues" runat="server"> 

</div> 

</form> 

</BODY> 

</HTML>

SelectMultiPages.aspx.cs

using System; 

using System.Collections; 

using System.ComponentModel; 

using System.Data; 

using System.Data.OleDb; 

using System.Drawing; 

using System.Web; 

using System.Web.SessionState; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Web.UI.HtmlControls;

namespace eMeng.Exam 



/// <summary> 

/// SelectMultiPages 的摘要说明。 

/// </summary> 

public class SelectMultiPages : System.Web.UI.Page 



protected System.Web.UI.WebControls.Button Button1; 

protected System.Web.UI.WebControls.Label Label1; 

protected System.Web.UI.HtmlControls.HtmlInputHidden HdnSelectedValues; 

protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e) 



// 在此处放置用户代码以初始化页面 

if(!Page.IsPostBack) 

BindData(); 



private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e) 



DataGrid1.CurrentPageIndex = e.NewPageIndex; 

BindData(); 

}

void BindData() 



OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 

+ HttpContext.Current.Server.MapPath("aspx.mdb")); 

OleDbDataAdapter da = new OleDbDataAdapter("Select Title, CreateDate from Document",cn); 

DataSet ds = new DataSet(); 

da.Fill(ds); 

DataGrid1.DataSource= ds; 

DataGrid1.DataBind(); 

}

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 



//又一次显示所选择的项目 

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 



if(HdnSelectedValues.Value.IndexOf(((Literal)e.Item.Cells[1].FindControl("TitleShow")).Text) >= 0 ) 



HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("ChkSelect")); 

ChkSelected.Checked = true; 







private void Button1_Click(object sender, System.EventArgs e) 



//为了显示的方便进行替换的 

Label1.Text = HdnSelectedValues.Value.Replace(",","<li>"); 

}

#region Web 窗口设计器生成的代码 

override protected void OnInit(EventArgs e) 



// 

// CODEGEN: 该调用是 ASP.NET Web 窗口设计器所必需的。 

// 

InitializeComponent(); 

base.OnInit(e); 

}

/// <summary> 

/// 设计器支持所需的方法 - 不要使用代码编辑器改动 

/// 此方法的内容。 

/// </summary> 

private void InitializeComponent() 



this.DataGrid1.ItemDataBound += 

new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); 

this.DataGrid1.PageIndexChanged += 

new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged); 

this.Button1.Click += new System.EventHandler(this.Button1_Click); 

this.Load += new System.EventHandler(this.Page_Load);



#endregion



}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

A在SP.NET跨页多选的更多相关文章

  1. Bootstrap table 跨页全选

    此代码是针对于bootstrap table中分页的跨页全选. 以下是整个bootstrap的定义 <script type="text/javascript" src=&q ...

  2. Element UI表格组件技巧:如何简洁实现跨页勾选、跨页统计功能

    业务场景 在使用Element UI的Table组件时,常常面对这样的业务需求: 表格数据的每一项都要提供勾选框,当切换分页时,能够记忆所有页面勾选的数据,以实现批量提交不同页面勾选数据的功能.并且, ...

  3. web跨页弹窗选值

    最近在项目中看到这样一种效果——点击当前网页文本框,然后弹出一个缩小的网页,并在网页里选择或填写数据,然后又返回当前网页,小网页关闭.感觉非常不错,其实在以前网上也看见过,只是当时没有留心.今天抽时间 ...

  4. element ui表格常用功能如:导出 新增 删除 多选 跨页多选 固定表头 多级表头 合并行列 等常见需求

    <template> <div class="table-cooperte"> <el-table :data="tableData&quo ...

  5. Candence下对“跨页连接器(off-page connector)”进行批量重命名的方法

    parts.ports.alias等等均可以在“属性编辑器(Property Editor)”中进行查看编辑,并通过复制到Excel等表格软件来进行批量修改.之后再粘贴回去的方法进行批量编辑.但是“跨 ...

  6. Office 2019 Word表格无法跨页重复标题行

    Office 2019 Word表格无法跨页重复标题行 今天使用Word设置表格枫叶重复标题行,死活无法实现 右键属性设置还是直接点击重复标题行设置,表格整个跳转到下一页去了 然后百度了解决方案是在[ ...

  7. layui table 跨页记忆选择

    layui 表格功能目前默认不支持跨页记忆选择 下面来实现layui table跨页记忆选择实现 基于layui版本 1.4.5 表格跨页通用方法 //表格分页复选框 layui.define(['j ...

  8. 网页打印A4纸-----表格在跨页时自动换页打印的实现 (转)

    在最近所做的一个项目中,需要通过网页来打印不少的表单,但是又不想每个打印页签各占用一个页面,这样就需要生存很多不同的冗余页面,为了减少冗余,所有的表单通过jquery的页签tab来实现的. 一 :基本 ...

  9. 解决SMARTFORMS 中table 控件单行跨页的问题

    在CX项目中,MM模块做了大量的的单据打印的工作,一个问题困扰了我好久,一直不能解决.当物料描述很长时,table控件在单元格中能自动换行,这样就有可能在换页处出现一行记录的一部分打在上一页,一部分记 ...

随机推荐

  1. Centos 6.5下一个SNMP简单配置(snmp protocol v3,监控宝)

    Centos 6.5下一个SNMP简单配置(snmp protocol v3.监控宝) jom_ch@2014/7/25 1,安装 >yum -y install net-snmp net-sn ...

  2. IOS-Plist文件存储(1)

    1.什么是一个文件系统? IOS每个应用程序都有自己的文件系统.并且有一个相应的接入,一般分 ~/Documents/ ~/tmp/ ~/Library/Caches/ ~/Library/Prefe ...

  3. WPF学习(9)样式和行为

    在asp.net世界中,我们的美工人员会为我们准备好静态页面,它注意包括三个部分:html.css和js.而在WPF世界里,也同样有着类似这三个部分的静态页面:Xaml.Style和Behaviors ...

  4. HTML5学习笔记简明版(10):过时的元素和属性

    被遗弃的元素(Element) 这个小节里列出的元素在HTML5里将不再使用,现有文档升级到 HTML5的话能够使用一些替代方案.比如parser section 能够处理isindex 元素的功能. ...

  5. 整理php操作memcache缓存为基础的方法

    php操作memcache共享缓存方法 采用memcache的前提下,是需要在服务器端被配置memcahche环境! 证实memcahce经过正常的连接可以在程序中使用! <?php /** * ...

  6. Cocos2d-X字体

    Cocos2d-X顺便文本显示在以下三个: CCLabelTTF: 使用系统字体.每一个字符串会生成一个纹理.显示效率比較低下,适合不变化的文字 CCLabelAtlas: 使用NodeAtlas优化 ...

  7. Tigase XMPP Server在CentOS部署和配置

    Tigase XMPP Server在CentOS部署与配置 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 以下讲述Tigase XMPP Server ...

  8. avalon与双缓冲技术

    avalon与双缓冲技术 avalon1.5一个重要技术升级是引进异步渲染.异步渲染在游戏界有一个更专业的名字,叫双缓冲.游戏界要刷新界面与我们刷新浏览器视图,面临的问题是一致的.视图是由许多存在套嵌 ...

  9. Python installation

    PIP:Python & OS Support pip works with CPython versions 2.6, 2.7, 3.2, 3.3, 3.4 and also pypy. p ...

  10. newlisp获得bash该命令的退出状态

    newlisp exec你可以运行bash命令.但如何返回状态来运行它? 特别是,我需要监控hdfs dfs -test 结果返回.经过一番摸索,我发现了一个简单的答案: #!/usr/bin/new ...