查看: 6140|回复: 0
打印 上一主题 下一主题

跨页面实现多选

[复制链接]
跳转到指定楼层
1#
发表于 2007-9-19 23:43:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
<%@ Page language="c#" Codebehind="ye.aspx.cs" AutoEventWireup="false" Inherits="CrystalReport.ye" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
   <HEAD>
       <title>跨页面实现多选</title>
       <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
       <meta name="CODE_LANGUAGE" Content="C#">
       <meta name="vs_defaultClientScript" content="JavaScript">
       <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
       <META http-equiv="content-type" content="text/html; charset=gb2312">
       <style>
       UNKNOWN { 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 MS_POSITIONING="GridLayout">
       <form id="SelectMultiPage" runat="server">
           <asp:datagrid id="DataGrid1" HorizontalAlign="Center" AutoGenerateColumns="False" Width="600px"
               AllowPaging="True" runat="server" PageSize="10">
               <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
               <HeaderStyle Font-Bold="True" HorizontalAlign="Center" BackColor="#AAAADD"></HeaderStyle>
               <Columns>
                   <asp:TemplateColumn HeaderText="选择">
                       <ItemTemplate>
                           <input type="checkbox" runat="server" id="chkSelect" onclick="AddRemoveValues(this)"
                   value='<%#DataBinder.Eval(Container.DataItem,"id")%>' NAME="chkSelect"/>
                       </ItemTemplate>
                   </asp:TemplateColumn>
                   <asp:TemplateColumn HeaderText="文章标识">
                       <ItemTemplate>
                           <asp:Literal Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' runat="server" ID="code"/>
                       </ItemTemplate>
                   </asp:TemplateColumn>
                   <asp:TemplateColumn HeaderText="发布时间">
                       <ItemTemplate>
                           <asp:Literal Text='<%# DataBinder.Eval(Container.DataItem, "createdate").ToString() %>' runat="server" ID="Literal1"/>
                       </ItemTemplate>
                   </asp:TemplateColumn>
               </Columns>
               <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
           </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>







using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

namespace CrystalReport
{
   /// <summary>
   /// ye 的摘要说明。
   /// </summary>
   public class ye : System.Web.UI.Page
   {
       protected System.Web.UI.WebControls.DataGrid DataGrid1;
       protected System.Web.UI.WebControls.Button Button1;
       protected System.Web.UI.WebControls.Label Label1;
       protected System.Web.UI.HtmlControls.HtmlInputHidden HdnSelectedValues;
   
       private void Page_Load(object sender, System.EventArgs e)
       {
           // 在此处放置用户代码以初始化页面
           if(!Page.IsPostBack)
               BindData();

       }

       #region Web 窗体设计器生成的代码
       override protected void OnInit(EventArgs e)
       {
           //
           // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
           //
           InitializeComponent();
           base.OnInit(e);
       }
      
       /// <summary>
       /// 设计器支持所需的方法 - 不要使用代码编辑器修改
       /// 此方法的内容。
       /// </summary>
       private void InitializeComponent()
       {   
           this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
           this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
           this.Button1.Click += new System.EventHandler(this.Button1_Click);
           this.Load += new System.EventHandler(this.Page_Load);

       }
       #endregion

       private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.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 id, 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("id")).Text+",")>= 0 )
               {
                   HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("ChkSelect"));
                   ChkSelected.Checked = true;
               }
           }
       }

       private void Button1_Click(object sender, System.EventArgs e)
       {
           string str;
        str = HdnSelectedValues.Value.Replace(",,","-");
          str = str.Replace(",","-");
           if(str.Length>0)
           {
               str=str.Substring(0,str.Length-1);
           }      
      this.Label1.Text=str.Replace("-","<li>");
           
       }
   }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表