我有像这样的 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 返回 null 。我想更改 Literal 控件的文本,但我不知道该怎么做。

有帮助吗?

解决方案

我相信除非你在代码中的某处调用 ListView DataBind 方法,否则 ListView 将永远不会尝试数据绑定。然后什么都不会呈现,甚至 Literal 控件也不会被创建。

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
        }    
    }
}

其他提示

您可以使用以下内容:

 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
                }
            }
        }

这不是你提出的具体问题,但另一种做这种事情的方法是:

<EmptyDataTemplate>
  <%= Foobar() %>
</EmptyDataTemplate>

在文件

的页面代码中定义了Foobar
public partial class MyClass : System.Web.UI.Page
{
...
    public string Foobar()
    {
         return "whatever";
    }
}

另一种方法......

<asp:ListView ID="ListView1" runat="server">
   <EmptyDataTemplate>
      <asp:Literal ID="Literal1" runat="server" text="some text" OnInit="Literal1_Init" />
   </EmptyDataTemplate>
   ...
</asp:ListView>

代码隐藏......

protected void Literal1_Init(object sender, EventArgs e)
{
    (sender as Literal).Text = "Some other text";
}
 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

...

回答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";
}

不幸的是,我没有找到一种不使用Controls [0]的方法。

在通常的Item事件(ItemDataBound或ItemCreate)中,您可以使用ListViewItemEventArgs的e.Item来获取ListViewItem。在DataBound事件中,只有一个通用的EventArgs。

最重要的是,似乎((控制)发送者).FindControl(&quot; Literal1&quot;)也不起作用(从树顶部的listview中查找控件),因此使用控件[0] .FindControl(...)(从listviewitem中找到控件)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top