回传时为什么Repeater控件中Literal控件无值(有示例)

Dic4000 2010-05-13 03:37:43
下面是代码,可以直接Copy到工程中运行:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Menu.aspx.cs" Inherits="Menu"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:LinkButton ID="btnAdd" runat="server" Text="增加" OnClick="btnAdd_Click"/>

<asp:Repeater ID="rept" runat="server" OnItemCreated="rept_ItemCreated" OnItemDataBound="rept_ItemDataBound" OnItemCommand="rept_ItemCommand">

<ItemTemplate>
<asp:Literal ID="Content" runat="server"/>
<asp:TextBox ID="tbx" runat="server"/>
<asp:LinkButton ID="btnUpdate" runat="server" Text="测试" CommandName="Update"/>
</ItemTemplate>
</asp:Repeater>

</form>
</body>
</html>



using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Collections;

public partial class Menu : System.Web.UI.Page
{

private string _cmd = DataControlCommands.SelectCommandName;



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList ls = new ArrayList();

rept.DataSource = ls;
rept.DataBind();

}
}

protected void rept_ItemCreated(object sender, RepeaterItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Header) {}
else if (e.Item.ItemType == ListItemType.Footer) {}
else
{
if (_cmd == DataControlCommands.NewCommandName)
{
Literal content = e.Item.FindControl("Content") as Literal;
content.Text = "It is a Literal";//content.Text的内容是动态的,且只能在点新增按钮时赋给content.Text,不能在其它地方给content.Text赋值.这里为了测试方便写了个固定值
TextBox tbx = e.Item.FindControl("tbx") as TextBox;
tbx.Text = "It is a TextBox";

}
}
}

protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
{


if (e.CommandName == DataControlCommands.UpdateCommandName)
{
Literal content = e.Item.FindControl("Content") as Literal;
TextBox tbx = e.Item.FindControl("tbx") as TextBox;
//为什么tbx.Text有值,而content.Text无值?
}
}

protected void rept_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

}

protected void btnAdd_Click(object sender, EventArgs e)
{
ArrayList ls = new ArrayList();
ls.Add("1");
_cmd = DataControlCommands.NewCommandName;
rept.DataSource = ls;
rept.DataBind();

}
}



1:在rept_ItemCommand方法中添加断点
2:运行程序,首先点增加按钮,然后点测试按钮,点测试按钮后,程序会进入rept_ItemCommand方法,这时会发现tbx.Text有值,而content.Text无值.
问题:为什么这时候content.Text会没有值?想让它保有以前的值该怎么做?
...全文
185 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dic4000 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sandy945 的回复:]

不看回帖?
[/Quote]
看了,不过对于我的疑问没什么帮助啊,我的content.Text是后台赋过去的,不是在页面上通过绑定语法绑定上去的,所以点测试按钮后是不会进入rept_ItemDataBound方法的.
但Liter_outside.Text会有值呢?
阿非 2010-05-14
  • 打赏
  • 举报
回复
没有丢失的原因是 ViewState

你可以设置Page 的 EnableViewState="false"

也可以设置 Literal 的 。

然后再试一下
Dic4000 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sandy945 的回复:]

#2 的回复是告诉你如何解决

#3 是告诉你原因
[/Quote]

Thanks,经过反复研读终于得到解决,但是原因还是有点模糊。
还有个小问题就是Repeater外围的Literal没有继承IPostBackDataHandler接口,回传后文本"这是一个外围的Literal"为什么没有丢失?
阿非 2010-05-14
  • 打赏
  • 举报
回复
#2 的回复是告诉你如何解决

#3 是告诉你原因
Dic4000 2010-05-14
  • 打赏
  • 举报
回复
自己顶下
阿非 2010-05-13
  • 打赏
  • 举报
回复
不看回帖?
Dic4000 2010-05-13
  • 打赏
  • 举报
回复
mngzilin:那个if语句不能去掉,因为去掉后,如果点测试按钮会又对Repeater绑定一次,这种情况下的测试是没意义的.
对于Literal控件在回发时是否会丢失运行时所设置的值,我做了如下的测试:
我在Repeater外面添加了另一个Literal和按钮,并在Page_Load方法里加了一个语句

<form id="form1" runat="server">
<asp:LinkButton ID="btnAdd" runat="server" Text="增加" OnClick="btnAdd_Click"/>
<asp:Repeater ID="rept" runat="server" OnItemCreated="rept_ItemCreated" OnItemDataBound="rept_ItemDataBound" OnItemCommand="rept_ItemCommand">
<ItemTemplate> <asp:Literal ID="Content" runat="server"/>
<asp:TextBox ID="tbx" runat="server"/>
<asp:LinkButton ID="btnUpdate" runat="server" Text="测试" CommandName="Update"/>
</ItemTemplate>
</asp:Repeater>

<p><asp:Literal ID="Liter_outside" runat="server"></asp:Literal></p>
<asp:Button ID="Button1" runat="server" Text="Out" />
</form>



protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
ArrayList ls = new ArrayList();

rept.DataSource = ls;
rept.DataBind();
Liter_outside.Text = "这是一个外围的Literal";//对外围的Literal控件赋值
}
}


程序运行后,点Out按钮,会发现 Liter_outside的值并没丢失.但是为什么Repeater里的Literal会丢失值呢?
mngzilin 2010-05-13
  • 打赏
  • 举报
回复
回发导致控件值丢失,TextBox实现了IPostBackDataHandler接口,回传后文本不会丢失。


private static string _cmd = DataControlCommands.SelectCommandName;

protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)去掉
{
ArrayList ls = new ArrayList();
ls.Add("1");

rept.DataSource = ls;
rept.DataBind();

}
}

阿非 2010-05-13
  • 打赏
  • 举报
回复
rept_ItemCreated

=>

rept_ItemDataBound
Dic4000 2010-05-13
  • 打赏
  • 举报
回复
自己顶下

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧