Multi-choice across pages in ASP. NET
This article describes how to implement multi-page Selection in ASP. NET. The specific idea is simple: Use hidden INPUT to remember the items selected each time. When binding data, check the saved values and display them in the DataGrid. The complete code and example are as follows:
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> multi-choice across pages </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 ){
// Note that the value you saved must be unique so that the error items are not replaced.
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 = "select">
<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 Date">
<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 the 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>
/// Abstract description of 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 the page
If (! Page. IsPostBack)
BindData ();
}
Private void datagrid#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 maid (object sender, System. Web. UI. WebControls. DataGridItemEventArgs e)
{
// Re-display the selected project
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 button#click (object sender, System. EventArgs e)
{
// For ease of display, replace
Label1.Text = HdnSelectedValues. Value. Replace (",", "<li> ");
}
# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Maid + =
New System. Web. UI. WebControls. Maid (this. Maid itemdatabound );
This. Maid + =
New System. Web. UI. WebControls. Maid pageindexchanged );
This. Button1.Click + = new System. EventHandler (this. button#click );
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
}
}