产品中心

新闻邮件订阅

IT要闻
产品评测
技巧推荐
社区精华
请输入您的e-mail地址:

使用技巧 - How to Use

 您的位置:首页 > 使用技巧 > 编程 软件
在ASP.NET中跨页面实现多选
佚名 论坛 | 2007-12-28 10:56:21 |

引:本文介绍如何在ASP.NET中实现多页面选择的问题。其具体思路很简单:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时,检查保存的值,再在DataGrid中进行选中显示。

  本文介绍如何在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" 字串1 
 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在窗体和窗体之间传送数据 2007-12-12 10:06:18
· Asp.net中防止用户多次登录的方法 2007-11-28 10:38:17
· 在Asp.net中使用HtmlArea编辑器 2007-11-16 10:16:30
 
 
PCWorld社区

网友评论
以下网友评论只代表网友个人观点,不代表PCWorld观点
发表内容:

SPONSORED LINKS

联想M930一体机体验专区
5月15日,联想率先在喷墨一体机市场上发布了针对soho用户和小型办公类市场为主打……
联想外设数码业务百城巡展
传承奥运品质 倾心百城服务
爱普生3LCD联盟
了解3LCD已经汇集了3LCD技术精英的联盟站点
全方位深入的存储资讯
从企业级到个人应用,从业界动态到大型评测,带您尽览存储风云
享受高质量低成本的照片打印
联想4330照片打印机,打印成本仅需1元
尚品·人生网
尚品·人生网,让优质生活与事业和谐平衡
中华部落阁
加入中华部落阁,记录你的新生活。
润眼电脑“3H高清润眼屏”
免费体验,火热报名,海尔润眼电脑系列。
万元级彩色激光一体机
爱普生彩色激光多功能一体机CX11系列专区
联想投影机全球同一品质
新联想一周年庆典 新投影共享全球同一品质
关于我们投稿指南 公告 注册 诚信与安全 规则与政策 友情链接 网站地图 IDG全球资源 业务联系 68130909-6865 《微电脑世界》2009刊例
Copyright(c) 2000-2008 pcworld.com.cn. All Rights Reserved. 京ICP备 05038969 号