返回首页
当前位置: 主页 > 编程语言 > C#教程 >

ASP.NET中跨页面实现多选 实现代码

时间:2014-11-10 23:21来源:电脑教程学习网 www.etwiki.cn 编辑:admin

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)

------分隔线----------------------------
标签(Tag):c# c#技巧 c#实例教程 c#源代码 c#基础教程
------分隔线----------------------------
推荐内容
猜你感兴趣