我觉得我的例子和MSDN (saucer(思归)版)的大概是一致的,个人觉得问题出在为什么我的页面的dlReplyList在绑定前(就是击编辑按钮后)会是“未定义的值”的?如下图(上半):而saucer(思归)版的例子,在同一个断点,DataList实例ItemsList却是存在的(下半)
页面框架:
DataKeyField="PostId" ID="dgPostList" runat="server"
AllowPaging="True" PageSize="10" AutoGenerateColumns="false"
OnPageIndexChanged="TrunPage" OnEditCommand="EditPost"
OnDeleteCommand=DeletePost OnUpdateCommand=UpdatePost OnCancelCommand=CancelPost>
...
......
DataKeyField="PostId" RepeatDirection=Vertical runat="server"
OnEditCommand="EditReply" OnDeleteCommand=DeleteReply
OnUpdateCommand=UpdateReply OnCancelCommand=CancelReply
DataSource=''>
.....
AlternateText="删除">
.......
..........
...
.......
AlternateText="保存">
.........
后台代码,看有色部分
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
{
///
/// index 的摘要说明。
///
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);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

