SelectMultiPages.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SelectMultiPages.aspx.cs"
2 Inherits="SelectMultiPages" %>
3
4<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
5<html>
6<head runat="server">
7 <title>跨页面实现多选</title>
8 <meta http-equiv="content-type" content="text/html; charset=gb2312">
9 <style>
10* {FONT-SIZE:12PX}
11#Status {text-align:left}
12</style>
13
14 <script language="JAVASCRIPT">
15 var tgs;
16 var tmp_background_val;
17 function tog(n,flags)
18 {
19 //傳過來本次點擊前的的背景顏色
20 if (tgs){
21 tgs.style.background= tmp_background_val ;
22 }
23 //考慮到連續點同一個對象的情況,就不要改變保留的顏色
24 n.style.background= '#99ccff' ;
25 tmp_background_val=flags;
26 tgs=n;
27 }
28 function AddRemoveValues(oChk)
29 {
30 //在处理这个地方需要注意的是:你保存的值应该具有唯一性,这样才能不会替换错误的项。
31 if(oChk.checked)
32 SelectMultiPage.HdnSelectedValues.value += "," + oChk.value;
33 else
34 SelectMultiPage.HdnSelectedValues.value = SelectMultiPage.HdnSelectedValues.value.replace("," + oChk.value,"");
35 }
36 </script>
37
38</head>
39<body>
40 <form id="SelectMultiPage" runat="server">
41 <asp:GridView ID="DataGrid1" HorizontalAlign="Center" AutoGenerateColumns="False"
42 Width="600px" AllowPaging="True" runat="server" OnRowDataBound="DataGrid1_RowDataBound"
43 OnPageIndexChanging="DataGrid1_PageIndexChanging">
44 <AlternatingRowStyle BorderColor="#EEEEEE" />
45 <HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center" />
46 <PagerSettings Mode="Numeric" Position="Bottom" Visible="true"/>
47 <Columns>
48 <asp:TemplateField HeaderText="选择">
49 <ItemTemplate>
50 <input type="checkbox" runat="server" id="chkSelect" onclick="AddRemoveValues(this)"
51 value='<%#Eval("Label")%>' />
52 </ItemTemplate>
53 </asp:TemplateField>
54 <asp:TemplateField HeaderText="页面标题">
55 <ItemTemplate>
56 <asp:Literal Text='<%# Eval("Label") %>' runat="server" ID="TitleShow" />
57 </ItemTemplate>
58 </asp:TemplateField>
59 <asp:TemplateField HeaderText="链接地址">
60 <ItemTemplate>
61 <asp:Literal ID="Literal1" Text='<%# Eval("Link") %>' runat="server" />
62 </ItemTemplate>
63 </asp:TemplateField>
64 </Columns>
65 </asp:GridView>
66 <div align="center">
67 <asp:Button ID="Button1" runat="server" Text="得到所选的值" OnClick="Button1_Click1"></asp:Button>
68 <div id="Status" align="center">
69 <asp:Label ID="Label1" runat="server"></asp:Label>
70 </div>
71 <input id="HdnSelectedValues" type="hidden" name="HdnSelectedValues" runat="server">
72 </div>
73 </form>
74</body>
75</html>
SelectMultiPages.aspx.cs2 Inherits="SelectMultiPages" %>
3
4<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
5<html>
6<head runat="server">
7 <title>跨页面实现多选</title>
8 <meta http-equiv="content-type" content="text/html; charset=gb2312">
9 <style>
10* {FONT-SIZE:12PX}
11#Status {text-align:left}
12</style>
13
14 <script language="JAVASCRIPT">
15 var tgs;
16 var tmp_background_val;
17 function tog(n,flags)
18 {
19 //傳過來本次點擊前的的背景顏色
20 if (tgs){
21 tgs.style.background= tmp_background_val ;
22 }
23 //考慮到連續點同一個對象的情況,就不要改變保留的顏色
24 n.style.background= '#99ccff' ;
25 tmp_background_val=flags;
26 tgs=n;
27 }
28 function AddRemoveValues(oChk)
29 {
30 //在处理这个地方需要注意的是:你保存的值应该具有唯一性,这样才能不会替换错误的项。
31 if(oChk.checked)
32 SelectMultiPage.HdnSelectedValues.value += "," + oChk.value;
33 else
34 SelectMultiPage.HdnSelectedValues.value = SelectMultiPage.HdnSelectedValues.value.replace("," + oChk.value,"");
35 }
36 </script>
37
38</head>
39<body>
40 <form id="SelectMultiPage" runat="server">
41 <asp:GridView ID="DataGrid1" HorizontalAlign="Center" AutoGenerateColumns="False"
42 Width="600px" AllowPaging="True" runat="server" OnRowDataBound="DataGrid1_RowDataBound"
43 OnPageIndexChanging="DataGrid1_PageIndexChanging">
44 <AlternatingRowStyle BorderColor="#EEEEEE" />
45 <HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center" />
46 <PagerSettings Mode="Numeric" Position="Bottom" Visible="true"/>
47 <Columns>
48 <asp:TemplateField HeaderText="选择">
49 <ItemTemplate>
50 <input type="checkbox" runat="server" id="chkSelect" onclick="AddRemoveValues(this)"
51 value='<%#Eval("Label")%>' />
52 </ItemTemplate>
53 </asp:TemplateField>
54 <asp:TemplateField HeaderText="页面标题">
55 <ItemTemplate>
56 <asp:Literal Text='<%# Eval("Label") %>' runat="server" ID="TitleShow" />
57 </ItemTemplate>
58 </asp:TemplateField>
59 <asp:TemplateField HeaderText="链接地址">
60 <ItemTemplate>
61 <asp:Literal ID="Literal1" Text='<%# Eval("Link") %>' runat="server" />
62 </ItemTemplate>
63 </asp:TemplateField>
64 </Columns>
65 </asp:GridView>
66 <div align="center">
67 <asp:Button ID="Button1" runat="server" Text="得到所选的值" OnClick="Button1_Click1"></asp:Button>
68 <div id="Status" align="center">
69 <asp:Label ID="Label1" runat="server"></asp:Label>
70 </div>
71 <input id="HdnSelectedValues" type="hidden" name="HdnSelectedValues" runat="server">
72 </div>
73 </form>
74</body>
75</html>
1using System;
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5using System.Collections;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13public partial class SelectMultiPages : System.Web.UI.Page
14{
15 protected SqlConnection conn;
16 protected SqlCommand comm;
17 protected MX.Data.Connection connstr = new MX.Data.Connection();
18 protected void Page_Load(object sender, System.EventArgs e)
19{
20 // 在此处放置用户代码以初始化页面
21 if(!Page.IsPostBack)
22 BindData();
23}
24
25void BindData()
26{
27 conn=connstr.SqlConnection;
28 SqlDataAdapter da=new SqlDataAdapter("Select ID,Label,Link from Sys_Page_Info",conn);
29 DataSet ds = new DataSet();
30 da.Fill(ds);
31 DataGrid1.DataSource= ds;
32 DataGrid1.DataBind();
33}
34
35Web 窗体设计器生成的代码
54
55 protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
56 {
57 //重新显示所选择的项目
58 if (e.Row.RowType == DataControlRowType.DataRow)
59 {
60 e.Row.Attributes.Add("onclick","tog(this,'AliceBlue')");
61 if (HdnSelectedValues.Value.IndexOf(((Literal)e.Row.Cells[1].FindControl("TitleShow")).Text) >= 0)
62 {
63 HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Row.Cells[0].FindControl("ChkSelect"));
64 ChkSelected.Checked = true;
65 }
66 }
67 }
68 protected void DataGrid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
69 {
70 DataGrid1.PageIndex = e.NewPageIndex;
71 BindData();
72 }
73 protected void Button1_Click1(object sender, EventArgs e)
74 {
75 //为了显示的方便进行替换的
76 Label1.Text = HdnSelectedValues.Value.Replace(",", "<li>");
77 }
78}
本文来自 : http://www.cnblogs.com/huangwen/archive/2007/02/09/645559.html
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5using System.Collections;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13public partial class SelectMultiPages : System.Web.UI.Page
14{
15 protected SqlConnection conn;
16 protected SqlCommand comm;
17 protected MX.Data.Connection connstr = new MX.Data.Connection();
18 protected void Page_Load(object sender, System.EventArgs e)
19{
20 // 在此处放置用户代码以初始化页面
21 if(!Page.IsPostBack)
22 BindData();
23}
24
25void BindData()
26{
27 conn=connstr.SqlConnection;
28 SqlDataAdapter da=new SqlDataAdapter("Select ID,Label,Link from Sys_Page_Info",conn);
29 DataSet ds = new DataSet();
30 da.Fill(ds);
31 DataGrid1.DataSource= ds;
32 DataGrid1.DataBind();
33}
34
35Web 窗体设计器生成的代码
54
55 protected void DataGrid1_RowDataBound(object sender, GridViewRowEventArgs e)
56 {
57 //重新显示所选择的项目
58 if (e.Row.RowType == DataControlRowType.DataRow)
59 {
60 e.Row.Attributes.Add("onclick","tog(this,'AliceBlue')");
61 if (HdnSelectedValues.Value.IndexOf(((Literal)e.Row.Cells[1].FindControl("TitleShow")).Text) >= 0)
62 {
63 HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Row.Cells[0].FindControl("ChkSelect"));
64 ChkSelected.Checked = true;
65 }
66 }
67 }
68 protected void DataGrid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
69 {
70 DataGrid1.PageIndex = e.NewPageIndex;
71 BindData();
72 }
73 protected void Button1_Click1(object sender, EventArgs e)
74 {
75 //为了显示的方便进行替换的
76 Label1.Text = HdnSelectedValues.Value.Replace(",", "<li>");
77 }
78}