This article describes how to implement multiple page selection in asp.net. The specific idea is simple: use hidden input to remember each item selected, in the data binding, check the saved values, and then in the DataGrid to select the display. Here's a complete code and example:
View examples
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> Cross-page implementation of multiple selection </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) {
//In dealing with this place it is important to note that the value you save should be unique so that you do not replace the wrong item.
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= "Choice" >
<ItemTemplate>
<input type= "checkbox" runat= "Server" id= "Chkselect" onclick= "addremovevalues (This)"
value= ' <% #DataBinder. Eval (Container.DataItem, Title)%> '/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "article title" >
<ItemTemplate>
<asp:literal text= ' <%# databinder.eval (Container.DataItem, "Title")%> ' runat= ' "Server" id= "TitleShow"/ >
</ItemTemplate>
</asp:TemplateColumn>
<asp:templatecolumn headertext= "Release Time" >
<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= "Get selected value" ></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>
Summary description of the
///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)
{
//Place user code here to initialize page
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)
{
//re-display the selected item
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)
{
///replace
for ease of display
Label1.Text = HdnSelectedValues.Value.Replace (",", "<li>");
}
#region The code generated by the Web Forms Designer
override protected void OnInit (EventArgs e)
{
//
//CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
base. OnInit (e);
}
///<summary>
///Designer supports the desired method-do not modify
with the Code Editor
///The contents of this method.
///</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
}
}