跨页面实现多选


本文介绍如何在ASP.NET中实现多页面选择的问题。其具体思路很简单:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时,检查保存的值,再在DataGrid中进行选中显示。下面时完整的代码和例子:
<也可以ViewState实现>
以下是程序图示


ages.aspx
&lt;%@ Page EnableViewState="true" CodeBehind="SelectMultiPages.aspx.cs" Language="c#" AutoEventWireup="false" Inherits="eMeng.Exam.SelectMultiPages" %&gt; &lt;!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;跨页面实现多选&lt;/title&gt; &lt;meta http-equiv="content-type" content="text/html; charset=gb2312"&gt; &lt;style&gt; * {FONT-SIZE:12PX} #Status {text-align:left} &lt;/style&gt; &lt;script language="JAVASCRIPT"&gt; function AddRemoveValues(oChk) { //在处理这个地方需要注意的是:你保存的值应该具有唯一性,这样才能不会替换错误的项。 if(oChk.checked) SelectMultiPage.HdnSelectedValues.value += "," + oChk.value; else SelectMultiPage.HdnSelectedValues.value = SelectMultiPage.HdnSelectedValues.value.replace("," + oChk.value,""); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="SelectMultiPage" runat="server"&gt; &lt;asp:datagrid id="DataGrid1" HorizontalAlign="Center" AutoGenerateColumns="False" Width="600px" AllowPaging="True" runat="server"&gt; &lt;alternatingitemstyle BackColor="#EEEEEE"&gt;&lt;/alternatingitemstyle&gt; &lt;headerstyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"&gt; &lt;/headerstyle&gt; &lt;pagerstyle HorizontalAlign="Right" Mode="NumericPages" Visible="True"&gt;&lt;/pagerstyle&gt; &lt;columns&gt; &lt;asp:templatecolumn HeaderText="选择"&gt; &lt;itemtemplate&gt; &lt;input type="checkbox" runat="server" id="chkSelect" onclick="AddRemoveValues(this)" value='&lt;%#databinder.eval(container.dataitem,"title")%&gt;'/&gt; &lt;/itemtemplate&gt; &lt;/asp:templatecolumn&gt; &lt;asp:templatecolumn HeaderText="文章标题"&gt; &lt;itemtemplate&gt; &lt;asp:literal Text='&lt;%# DataBinder.Eval(Container.DataItem, "Title") %&gt;' runat="server" ID="TitleShow"/&gt; &lt;/itemtemplate&gt; &lt;/asp:templatecolumn&gt; &lt;asp:templatecolumn HeaderText="发布时间"&gt; &lt;itemtemplate&gt; &lt;asp:literal Text='&lt;%# DataBinder.Eval(Container.DataItem, "CreateDate").ToString() %&gt;' runat="server"/&gt; &lt;/itemtemplate&gt; &lt;/asp:templatecolumn&gt; &lt;/columns&gt; &lt;/asp:datagrid&gt; &lt;div align=center&gt; &lt;asp:button id="Button1" runat="server" Text="得到所选的值"&gt;&lt;/asp:button&gt; &lt;div id="Status"&gt; &lt;asp:label id="Label1" runat="server"&gt;&lt;/asp:label&gt; &lt;/div&gt; &lt;input id="HdnSelectedValues" type="hidden" name="HdnSelectedValues" runat="server"&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; ----------------------------------------------------------------------------------------------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 { /// &lt;summary&gt; /// SelectMultiPages 的摘要说明。 /// &lt;/summary&gt; 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) &gt;= 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(","," &lt;li&gt;"); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// &lt;summary&gt; /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// &lt;/summary&gt; 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 } }
posted @ 2006-05-09 17:53  RicoRui  阅读(252)  评论(0编辑  收藏  举报