在ASP.NET中实现多页面选择的问题
2024-07-10 12:54:54
供稿:网友
本文介绍如何在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"
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"
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
}
}