質問

このような 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

...

Answering Broamの質問&quot;データバインドメソッドでこれを行う方法はありますか? 「controls [0]」をハードコードしたくないずさんなので」&quot;

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のみがあります。

さらに、((Control)sender).FindControl(&quot; Literal1&quot;)も機能していないようです(ツリーの上部にあるリストビューからコントロールを見つけてください)。 [0] .FindControl(...)(listviewitemからコントロールを見つける)。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top