在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. Freemarker详细解释

    A概念 最经常使用的概念 1. scalars:存储单值 字符串:简单文本由单或双引號括起来. 数字:直接使用数值. 日期:通常从数据模型获得 布尔值:true或false,通常在<#if -& ...

  2. Android:抄QQ照片选择器(按相册类别显示,加入选择题)

    这个例子的目的是为了实现类似至QQ照片选择功能.选择照片后,,使用类似新浪微博 微博 页面上显示. 先上效果图:     本例中使用的主要技术: 1.使用ContentProvider读取SD卡全部图 ...

  3. CSDN博客导出工具 Mac By Swift

    写这篇文章的主要目的是了解Swift语言本身,如何以及Objc和第三方交互框架 必须先用CSDN帐户登录.您可以导出所有的博客文章,加入YAML当首标信息,包括对应标签和分类在头制品信息,和底座式(原 ...

  4. Python 提取Twitter转发推文的元素(比方username)

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-24 @author: guaguastd @name: e ...

  5. Chromium on Android: Android在系统Chromium为了实现主消息循环分析

    总结:刚开始接触一个Chromium on Android时间.很好奇Chromium主消息循环是如何整合Android应用. 为Android计划,一旦启动,主线程将具有Java消息层循环处理系统事 ...

  6. SQL Server 备份和还原

    SQL Server 备份和还原   SQL Server 备份 恢复模式 SQL Server 数据恢复模式分为三种:完整恢复模式.大容量日志恢复模式.简单恢复模式. 完整恢复模式 默认的恢复模式, ...

  7. Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same entity解决方法

    1.错误信息 Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUs ...

  8. OCP读书笔记(23) - 题库(ExamC)

    200.Which operation requires that you create an auxiliary instance manually before executing the ope ...

  9. EF调用sp,EF自动生成返回类型

    在sp中添加下面的红色部分,就是执行sp时的返回类型,后面在EF中添加该sp后,EF会在DBContext文件中,自动生成调用该sp的代码,包括返回类型等,如下: public virtual Obj ...

  10. 阐述linux IPC(两):基于socket进程间通信(下一个)

    [版权声明:尊重原创.转载请保留源:blog.csdn.net/shallnet 要么 .../gentleliu,文章学习交流.请勿用于商业用途]     其中两个进程通信,有两个过程,以彼此的过程 ...