分享
 
 
 

我的DataGrid嵌套DataList(未解决)

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

我觉得我的例子和MSDN (saucer(思归)版)的大概是一致的,个人觉得问题出在为什么我的页面的dlReplyList在绑定前(就是击编辑按钮后)会是“未定义的值”的?如下图(上半):而saucer(思归)版的例子,在同一个断点,DataList实例ItemsList却是存在的(下半)

页面框架:

<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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有