上一篇:虚拟主机如何提高整体性能-application的使用 >>
我的DataGrid嵌套DataList(未解决)
页面框架:
<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
}
}
下一篇:大文件上传研究 >>
相关文章:
- · asp程序错误详细说明例表
- · 用好活动字幕标记—marquee
- · asp.net实现分组全选部分复选框
- · 在PPC上编译ASP,吃惊!!!
- · Asp之Request对象(1)
- · 汉字转拼音
- · 在ASP文件中调用DLL
- · 存储过程介绍及asp存储过程的使用
- · 如何把ASP编写成DLL
- · Asp.Net下导出/导入规则的Excel(.xls)文件
- · 我写ASP时常用到的一些函数(2)
- · asp 中处理异常
- · asp
- · ASP.Net Web Page深入探讨
- · IIS6 和Tomcat5 的整合
- · 浅谈如何建立三层体系结构的ASP应用程序
- · ASP控制每页打印行数
- · 对Blog的个性化修改
- · 日曆控件
- · 网页设计的价格标准
- · 在ASP.NET里通过URL传递参数得到乱码的解决方法
- · 常用ASP代码(一)
- · ASP分页代码,已经写成类了,值得参考
- · ASP错误处理
- · 带右键菜单的树形控件(1)
- · 我写ASP时常用到的一些函数
- · 金额转换算法
- · IIS防止文件下载完全手册(非更改文件名法)
- · Adodb.Command 平时很少注意到的一个参数
- · 一个获得随机字符串的asp函数
- · vbscript函数集
- · asp之数学函数
- · 会员管理系统中会员类的实现
- · Recordset对象方法详解
- · ASP.NET 2.0 的内部变化
- · asp之日期和时间函数示例
- · asp之字符串函数示例
- · asp之转换函数示例
