回传时为什么Repeater控件中Literal控件无值(有示例)
下面是代码,可以直接Copy到工程中运行:
1:在rept_ItemCommand方法中添加断点
2:运行程序,首先点增加按钮,然后点测试按钮,点测试按钮后,程序会进入rept_ItemCommand方法,这时会发现tbx.Text有值,而content.Text无值.
问题:为什么这时候content.Text会没有值?想让它保有以前的值该怎么做?
<%@ 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会没有值?想让它保有以前的值该怎么做?
...全文
相关推荐
Repeater的FooterTemplate中控件内容设置方法
Repeater的FooterTemplate中控件内容设置方法,需要的朋友可以参考下。
Repeater中嵌套Repeater的示例介绍
在某些特殊情况下是需要在Repeater中嵌套使用Repeater的,下面有个不错的示例,感兴趣的朋友可以参考下
实现repeater控件分页
C#实现repeater控件分页,初学者必熟练的控件。好资料大家分享。
Repeater控件实现分页代码
Repeater控件实现分页代码,很简单,很漂亮。
Repeater时间判断[源码]
本文介绍了在使用Repeater控件绑定数据时,如何对时间字段进行判断并实现样式变化。具体实现方法是在ItemDataBound事件中,通过比较数据中的时间与当前系统时间,计算时间差。如果时间差超过3天,则将日期文字显示为红色。代码示例展示了如何获取时间字段、计算时间差,并通过Literal控件动态设置显示文本。此外,还涉及了阿拉伯数字转换为中文大写的辅助类使用。


