页面框架:
<asp:datagrid width="80%"
datakeyfield="postid" id="dgpostlist" runat="server"
allowpaging="true" pagesize="10" autogeneratecolumns="false"
onpageindexchanged="trunpage" oneditcommand="editpost"
ondeletecommand=deletepost onupdatecommand=updatepost oncancelcommand=cancelpost>
<pagerstyle mode="numericpages" position="topandbottom" horizontalalign="center"></pagerstyle>
<columns>
<asp:templatecolumn>
<itemtemplate>
<table id="tbpostrecord" bgcolor="#dfdfdf" width="100%">
...
<div align="right">
<asp:imagebutton runat="server" id="imgbtedit" commandname="edit" imageurl="images/edit.gif" alternatetext="编辑"></asp:imagebutton>
<asp:imagebutton runat="server" id="imgbtdelete" commandname="delete" imageurl="images/del.gif" alternatetext="删除"></asp:imagebutton>
......
<asp:datalist id="dlreplylist" width="100%" repeatcolumns=1
datakeyfield="postid" repeatdirection=vertical runat="server"
oneditcommand="editreply" ondeletecommand=deletereply
onupdatecommand=updatereply oncancelcommand=cancelreply
datasource=<%# getreplydata((int)databinder.eval(container.dataitem, "postid"))%>>
<itemtemplate>
<table id="tbreplyrecord" width="100%">
.....
<asp:imagebutton runat="server" id="imgbtedit2" commandname="edit" imageurl="images/edit.gif" alternatetext="编辑"></asp:imagebutton>
<asp:imagebutton runat="server" id="imgbtdelete2" commandname="delete" imageurl="images/del.gif"
alternatetext="删除"></asp:imagebutton>
.......
</table>
</itemtemplate>
<edititemtemplate>
<table id="tbreplyrecordedit" width="100%">
..........
<asp:imagebutton runat="server" id="imgbtsave" commandname="update" imageurl="images/save.jpg" alternatetext="保存"></asp:imagebutton>
<asp:imagebutton runat="server" id="imgbtcancel" commandname="cancel" imageurl="images/cancel.jpg" alternatetext="取消"></asp:imagebutton>
...
</table>
</edititemtemplate>
</asp:datalist>
</itemtemplate>
<edititemtemplate>
<table id="tbpostrecordedit" bgcolor="#dfdfdf" width="100%">
.......
<asp:imagebutton runat="server" id="imgbtsavepost" commandname="update" imageurl="images/save.jpg"
alternatetext="保存"></asp:imagebutton>
<asp:imagebutton runat="server" id="imgbtcancelpost" commandname="cancel" imageurl="images/cancel.jpg" alternatetext="取消"></asp:imagebutton>
.........
</table>
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
后台代码,看有色部分
using system;
using system.data;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using fivexdotnet.bll;
namespace fivexdotnet.web
{
/// <summary>
/// index 的摘要说明。
/// </summary>
public class index : scrollpage
{
protected system.web.ui.webcontrols.label lblname;
protected system.web.ui.webcontrols.label lblpostid;
protected system.web.ui.webcontrols.label lblposttime;
protected system.web.ui.webcontrols.label lblcontent;
protected system.web.ui.webcontrols.label lblpostername;
protected system.web.ui.webcontrols.label lblreplytime;
protected system.web.ui.webcontrols.label lblreplycontent;
protected system.web.ui.webcontrols.datagrid dgpostlist;
protected system.web.ui.webcontrols.datalist dlreplylist;
protected system.web.ui.htmlcontrols.htmltextarea txanewpost;
protected dataset _ds = new dataset();
private void page_load(object sender, system.eventargs e)
{
if(!page.ispostback){
bind();
}
//string s = this.request.form["txanewpost"];
//string s01 = this.request.form["txt01"];
//string s02 = this.request.form["txanewpost"];
}
private void bind(){
post.getallpost(ref _ds);
dataview dvpostlist = new dataview(_ds.tables[0], "posttype = 0","postid desc",dataviewrowstate.currentrows);
dgpostlist.datasource = dvpostlist;
dgpostlist.databind();
}
protected dataview getreplydata(int ipostid){
dataview dvpostlist = new dataview(_ds.tables[0],"posttype = " + ipostid.tostring(),"postid",dataviewrowstate.currentrows);
//dataview dvpostlist = new dataview(_ds.tables[0]);
//dvpostlist.rowfilter = "posttype = " + ipostid.tostring();
return dvpostlist;
}
protected void trunpage(object sender, datagridpagechangedeventargs e){
dgpostlist.currentpageindex = e.newpageindex;
dgpostlist.edititemindex = -1;
this.bind();
}
protected void editreply(object sender, system.web.ui.webcontrols.datalistcommandeventargs e){
datalist dlreplylist = (datalist)sender;
dlreplylist.edititemindex = e.item.itemindex;
dgpostlist.edititemindex = -1;
datagriditem dgi = (datagriditem)dlreplylist.parent.parent;
int n = dgi.itemindex;
int npostid = (int)dgpostlist.datakeys[n];
post.getallpost(ref _ds);
dlreplylist.datasource = getreplydata(npostid);
dlreplylist.databind();
//dl.databind();
//dlreplylist.edititemindex = e.item.itemindex;
//this.dlreplylist = dl;
//post.getallpost(ref _ds);
//this.dlreplylist.datasource = getreplydata(22);
//this.dlreplylist.databind();
//this.bind();
}
protected void editpost(object sender, system.web.ui.webcontrols.datagridcommandeventargs e){
dgpostlist.edititemindex = e.item.itemindex;
this.bind();
}
protected void addpost(object sender, system.web.ui.imageclickeventargs e){
//string scontent = this.txanewpost.value.tostring();//为什么用编程方式取不到值的?
string scontent = this.request.form["txanewpost"];
//session["classmateid"] = 1;
post post = new post(convert.toint32(session["classmateid"]), datetime.now, 0, scontent);
post.add();
this.bind();
}
public void deletepost(object sender, datagridcommandeventargs e)
{
int ipostid = convert.toint32(dgpostlist.datakeys[(int)e.item.itemindex]);
post.delete(ipostid);
this.bind();
}
public void deletereply(object sender, datalistcommandeventargs e)
{
int ipostid = convert.toint32(dlreplylist.datakeys[(int)e.item.itemindex]);
post.delete(ipostid);
this.bind();
}
public void updatepost(object sender, datagridcommandeventargs e)
{
int ipostid = convert.toint32(dgpostlist.datakeys[(int)e.item.itemindex]);
string scontent = ((textbox)e.item.findcontrol("txtpostcontent")).text;
datetime dtposttime = datetime.now;
post post = new post(ipostid, dtposttime, scontent);
post.update();
dgpostlist.edititemindex = -1;
this.bind();
}
public void updatereply(object sender, datalistcommandeventargs e)
{
int ipostid = convert.toint32(dlreplylist.datakeys[(int)e.item.itemindex]);
string scontent = ((textbox)e.item.findcontrol("txtreplyconten")).text;
datetime dtposttime = datetime.now;
post post = new post(ipostid, dtposttime, scontent);
post.update();
dlreplylist.edititemindex = -1;
this.bind();
}
public void cancelpost(object sender, datagridcommandeventargs e)
{
dgpostlist.edititemindex = -1;
this.bind();
}
public void cancelreply(object sender, datalistcommandeventargs e)
{
dlreplylist.edititemindex = -1;
this.bind();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




