Multiple selections across pages in asp.net

Source: Internet
Author: User
Tags eval
Asp.net| Page 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>
<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)
{
To display the selected item again
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)
{
For the convenience of the display.
Label1.Text = HdnSelectedValues.Value.Replace (",", "<li>");
}

Code generated #region 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 required methods-do not use the Code editor to modify
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

}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.