datagrid跨页实现多选

博客介绍了datagrid跨页实现多选的方法,包含aspx和cs代码。在aspx中定义了datagrid及相关样式和脚本,用于处理多选逻辑;cs代码实现了数据绑定、页面索引改变处理、重新显示所选项目等功能,以实现跨页多选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif<!-- aspx的 -->
ExpandedBlockStart.gifContractedBlock.gif
<%dot.gif@ Page EnableViewState="true" CodeBehind="SelectMultiPages.aspx.cs" Language="c#" 
ExpandedBlockEnd.gifAutoEventWireup
="false" Inherits="eMeng.Exam.SelectMultiPages" 
%>
None.gif
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
None.gif
<HTML>
None.gif
<HEAD>
None.gif
<title>跨页面实现多选</title>
None.gif
<META http-equiv="content-type" content="text/html; charset=gb2312">
ExpandedBlockStart.gifContractedBlock.gif
<style>dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
{dot.gif}{FONT-SIZE:12PX}
ExpandedSubBlockStart.gifContractedSubBlock.gif#Status 
{dot.gif}{text-align:left}
None.gif
</style>
ExpandedBlockStart.gifContractedBlock.gif
<script language="JAVASCRIPT">dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function AddRemoveValues(oChk) dot.gif
InBlock.gif
//在处理这个地方需要注意的是:你保存的值应该具有唯一性,这样才能不会替换错误的项。
InBlock.gif
if(oChk.checked)
InBlock.gifSelectMultiPage.HdnSelectedValues.value 
+= "," + oChk.value; 
InBlock.gif
else
InBlock.gifSelectMultiPage.HdnSelectedValues.value 
= SelectMultiPage.HdnSelectedValues.value.replace("," + oChk.value,""); 
ExpandedBlockEnd.gif}

None.gif
</script>
None.gif
</HEAD>
None.gif
<BODY>
None.gif
<form id="SelectMultiPage" runat="server">
None.gif
<asp:datagrid id="DataGrid1" HorizontalAlign="Center" AutoGenerateColumns="False" Width="600px"
None.gif        AllowPaging
="True" runat="server">
None.gif    
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
None.gif    
<HeaderStyle BackColor="#AAAADD" Font-Bold="True" HorizontalAlign="Center"></HeaderStyle>
None.gif    
<PagerStyle HorizontalAlign="Right" Mode="NumericPages" Visible="True"></PagerStyle>
None.gif    
<Columns>
None.gif        
<asp:TemplateColumn HeaderText="选择">
None.gif            
<ItemTemplate>
None.gif                
<input type="checkbox" runat="server" id="chkSelect" onclick="AddRemoveValues(this)"
None.gif                    value
='<%#DataBinder.Eval(Container.DataItem,"Title")%>'/>
None.gif            
</ItemTemplate>
None.gif        
</asp:TemplateColumn>
None.gif        
<asp:TemplateColumn HeaderText="文章标题">
None.gif            
<ItemTemplate>
None.gif                
<asp:Literal Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" ID="TitleShow"/>
None.gif            
</ItemTemplate>
None.gif        
</asp:TemplateColumn>
None.gif        
<asp:TemplateColumn HeaderText="发布时间">
None.gif            
<ItemTemplate>
None.gif                
<asp:Literal Text='<%# DataBinder.Eval(Container.DataItem, "CreateDate").ToString() %>' runat="server"/>
None.gif            
</ItemTemplate>
None.gif        
</asp:TemplateColumn>
None.gif    
</Columns>
None.gif
</asp:datagrid>
None.gif
<div align=center>
None.gif
<asp:button id="Button1" runat="server" Text="得到所选的值"></asp:button>
None.gif
<div id="Status">
None.gif
<asp:label id="Label1" runat="server"></asp:label>
None.gif
</div>
None.gif
<INPUT id="HdnSelectedValues" type="hidden" name="HdnSelectedValues" runat="server">
None.gif
</div>
None.gif
</form>
None.gif
</BODY>
None.gif
</HTML>
None.gif
None.gif
None.gif//cs的
None.gif
using System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Data.OleDb;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
None.gif
namespace eMeng.Exam
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// SelectMultiPages 的摘要说明。
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic class SelectMultiPages : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif
protected System.Web.UI.HtmlControls.HtmlInputHidden HdnSelectedValues;
InBlock.gif
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif
InBlock.gif
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
// 在此处放置用户代码以初始化页面
InBlock.gif
    if(!Page.IsPostBack)
InBlock.gif    BindData();
ExpandedSubBlockEnd.gif}

InBlock.gif
private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    DataGrid1.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif    BindData(); 
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
void BindData()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    OleDbConnection cn 
= new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
InBlock.gif        
+ HttpContext.Current.Server.MapPath("aspx.mdb"));
InBlock.gif    OleDbDataAdapter da 
= new OleDbDataAdapter("Select Title, CreateDate from Document",cn);
InBlock.gif    DataSet ds 
= new DataSet();
InBlock.gif    da.Fill(ds);
InBlock.gif    DataGrid1.DataSource
= ds;
InBlock.gif    DataGrid1.DataBind();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
//重新显示所选择的项目
InBlock.gif
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(HdnSelectedValues.Value.IndexOf(((Literal)e.Item.Cells[1].FindControl("TitleShow")).Text) >= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HtmlInputCheckBox ChkSelected 
= (HtmlInputCheckBox)(e.Item.Cells[0].FindControl("ChkSelect"));
InBlock.gif            ChkSelected.Checked 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
//为了显示的方便进行替换的
InBlock.gif
    Label1.Text = HdnSelectedValues.Value.Replace(",","<li>");
ExpandedSubBlockEnd.gif}

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
//
InBlock.gif    
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif    
//
InBlock.gif
    InitializeComponent();
InBlock.gif    
base.OnInit(e);
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif
/// 此方法的内容。
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifprivate void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{    
InBlock.gif    
this.DataGrid1.ItemDataBound += 
InBlock.gif        
new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
InBlock.gif    
this.DataGrid1.PageIndexChanged += 
InBlock.gif        
new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
InBlock.gif    
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif    
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif
#endregion
    
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif

--------------转自孟宪会------------

转载于:https://www.cnblogs.com/acelove/archive/2005/03/17/120254.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值