2009-03-05 68 views
11

查找控制我有一個ListView這樣ListView中EmptyDataTemplate

<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。我想’ d想改變Literal控件的文字,但我不知道該怎麼做。

回答

19

我相信,除非您撥打ListView的代碼後面的DataBind方法ListView永遠不會嘗試數據綁定。然後什麼都不會呈現,甚至Literal控件贏得’ t創建。

在你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 
     }  
    } 
} 
+0

+1 - 這正是我所需要的。謝謝! – jonsidnell 2009-06-15 13:05:05

+4

在數據綁定方法中有沒有辦法做到這一點?我寧願*不*硬編碼「控件[0]」,因爲這是馬虎。 – Broam 2010-02-10 20:57:25

3

它不是具體你問什麼,但另一種方法做這種事情是這樣的:

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

其中Foobar的定義在你頁面的代碼文件後面

public partial class MyClass : System.Web.UI.Page 
{ 
... 
    public string Foobar() 
    { 
     return "whatever"; 
    } 
} 
4

你可以使用下面荷蘭國際集團:

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

...

2

的另一種方法...

<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"; 
} 
0

回答Broam的問題「在數據綁定方法中有沒有辦法做到這一點?我寧願不硬編碼「的控制[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"; 
} 

不幸的是,我還沒有找到一種方法,不使用控件[0]。

在平時的項目活動( ItemDataBound或ItemCreate),你可以使用ListViewItemEventArgs的e.Item來獲取ListViewItem,在DataBound事件中,只有一個通用的EventArgs,並且最重要的是((Control)sender)。 FindControl(「Literal1」)也不起作用(從樹頂部的listview查找控件),因此使用Controls [0] .FindControl(...)(從listviewitem中查找控件)。

相關問題