| 導購 | 订阅 | 在线投稿
分享
 
 
 

在ASP.NET中跨頁面實現多選

2007-02-07 12:19:00  編輯來源:互聯網  简体版  手機版  移動版  評論  字體: ||

本文介紹如何在ASP.NET中實現多頁面選擇的問題。其具體思路很簡單:用隱藏的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

}

}

本文介紹如何在ASP.NET中實現多頁面選擇的問題。其具體思路很簡單:用隱藏的INPUT記住每次選擇的項目,在進行數據綁定時,檢查保存的值,再在DataGrid中進行選中顯示。下面時完整的代碼和例子: [url=http://dotnet.aspx.cc/Exam/SelectMultiPages.aspx]查看例子[/url] 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 } }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有