C# 在 ListView EmptyDataTemplate 中查找控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/613277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find control in ListView EmptyDataTemplate
提问by Caline
I have the a ListView
like this
我有ListView
这样的
<asp:ListView ID="ListView1" runat="server">
<EmptyDataTemplate>
<asp:Literal ID="Literal1" runat="server" text="some text"/>
</EmptyDataTemplate>
...
</asp:ListView>
In Page_Load()
I have the following:
在Page_Load()
我有以下内容:
Literal x = (Literal)ListView1.FindControl("Literal1");
x.Text = "other text";
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.
但x
返回null
。我想更改Literal
控件的文本,但我不知道该怎么做。
采纳答案by Mcbeev
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.
我相信除非你在后面的代码中调用你的DataBind
方法,否则永远不会尝试数据绑定。然后什么都不会渲染,甚至不会创建控件。ListView
ListView
Literal
In your Page_Load
event try something like:
在您的Page_Load
活动中,请尝试以下操作:
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
}
}
}
回答by mhenry1384
It's not specifically what you asked, but another way to do that kind of thing is like this:
这不是你问的具体问题,但另一种做这种事情的方法是这样的:
<EmptyDataTemplate>
<%= Foobar() %>
</EmptyDataTemplate>
where Foobar is defined in your page's code behind file
Foobar 在页面的代码隐藏文件中定义的位置
public partial class MyClass : System.Web.UI.Page
{
...
public string Foobar()
{
return "whatever";
}
}
回答by avani gadhai
You can use the following:
您可以使用以下内容:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.EmptyItem)
{
Control c = e.Item.FindControl("Literal1");
if (c != null)
{
//this will atleast tell you if the control exists or not
}
}
}
回答by Anil Diggiwal
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
Dim searchValue As String = Replace(Request.QueryString("s"), "", "'")
Dim searchLiteral2 As Literal = CType(ListView1.FindControl("Literal2"), Literal)
searchLiteral2.Text = "''" & searchValue & "''"
End Sub
...
...
回答by Stuart
An alternative approach...
另一种方法...
<asp:ListView ID="ListView1" runat="server">
<EmptyDataTemplate>
<asp:Literal ID="Literal1" runat="server" text="some text" OnInit="Literal1_Init" />
</EmptyDataTemplate>
...
</asp:ListView>
In code-behind...
在代码隐藏...
protected void Literal1_Init(object sender, EventArgs e)
{
(sender as Literal).Text = "Some other text";
}
回答by Thierry_S
Answering Broam's question "Is there any way to do this in the databound method? I'd rather not hardcode "controls[0]" as that's sloppy"
回答 Broam 的问题“有没有办法在数据绑定方法中做到这一点?我宁愿不硬编码“controls[0]”,因为那很草率”
protected void ListView1_DataBound(object sender, EventArgs e)
{
ListView mylist = ((ListView)sender);
ListViewItem lvi = null;
if (mylist.Controls.Count == 1)
lvi = mylist.Controls[0] as ListViewItem;
if (lvi == null || lvi.ItemType != ListViewItemType.EmptyItem)
return;
Literal literal1 = (Literal)lvi.FindControl("Literal1");
if (literal1 != null)
literal1.Text = "No items to display";
}
Unfortunately, I've not found a way to not use Controls[0].
不幸的是,我还没有找到不使用 Controls[0] 的方法。
In the usual Item events (ItemDataBound or ItemCreate), you can use e.Item of the ListViewItemEventArgs to get the ListViewItem. In the DataBound event, there's only a generic EventArgs.
在通常的Item事件(ItemDataBound或ItemCreate)中,可以使用ListViewItemEventArgs的e.Item来获取ListViewItem。在 DataBound 事件中,只有一个通用的 EventArgs。
And on top of that, it seems that ((Control)sender).FindControl("Literal1") doesn't work either (find control from the listview at the top of the tree), hence the use of Controls[0] .FindControl(...) (find control from the listviewitem).
最重要的是,似乎 ((Control)sender).FindControl("Literal1") 也不起作用(从树顶部的列表视图中找到控件),因此使用 Controls[0] 。 FindControl(...)(从列表视图项中查找控件)。