由网友(ヾ隔ぜ燈火)分享简介:我有一个的ListView 像这样我有一个
的ListView
像这样
<asp:ListView ID="ListView1" runat="server">
<EmptyDataTemplate>
<asp:Literal ID="Literal1" runat="server" text="some text"/>
</EmptyDataTemplate>
...
</asp:ListView>
在的Page_Load()
我有以下几点:
Literal x = (Literal)ListView1.FindControl("Literal1");
x.Text = "other text";
但 X
返回空
。 I&rsquo的;倒要修改文字
控件的文本,但我不和rsquo的;吨有不知道该怎么做。
but x
returns null
. I’d like to change the text of the Literal
control but I don’t have no idea how to do it.
推荐答案
我认为,除非你调用的的DataBind
方法,你的的ListView
在身后code某处时,的ListView
将永远不会尝试将数据绑定。那么什么都不会渲染,甚至文字
控制荣获&rsquo的; T为创建
I believe that unless you call the DataBind
method of your ListView
somewhere in code behind, the ListView
will never try to data bind. Then nothing will render and even the Literal
control won’t be created.
在你的的Page_Load
事件试着这么做:
In your Page_Load
event try something like:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//ListView1.DataSource = ...
ListView1.DataBind();
//if you know its empty empty data template is the first parent control
// aka Controls[0]
Control c = ListView1.Controls[0].FindControl("Literal1");
if (c != null)
{
//this will atleast tell you if the control exists or not
}
}
}
相关推荐
最新文章