Question

Comment accéder à un contrôle dans le LayoutTemplate d'un contrôle ListView ?

Je dois accéder à litControlTitle et définir son attribut Text .

<asp:ListView ID="lv" runat="server">
  <LayoutTemplate>
    <asp:Literal ID="litControlTitle" runat="server" />
    <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
  </LayoutTemplate>
  <ItemTemplate>
  </ItemTemplate>
</asp:ListView>

Des pensées? Peut-être via l'événement OnLayoutCreated ?

Était-ce utile?

La solution

Essayez ceci:

((Literal)lv.FindControl("litControlTitle")).Text = "Your text";

Autres conseils

La solution complète:

<asp:ListView ID="lv" OnLayoutCreated="OnLayoutCreated" runat="server">
  <LayoutTemplate>
    <asp:Literal ID="lt_Title" runat="server" />
    <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
  </LayoutTemplate>
  <ItemTemplate>
  </ItemTemplate>
</asp:ListView>

Dans codebehind:

protected void OnLayoutCreated(object sender, EventArgs e)
{
    (lv.FindControl("lt_Title") as Literal).Text = "Your text";
}

Cette technique fonctionne pour la disposition de modèle; utilisez l'événement init du contrôle:

<asp:ListView ID="lv" runat="server" OnDataBound="lv_DataBound">
  <LayoutTemplate>
    <asp:Literal ID="litControlTitle" runat="server" OnInit="litControlTitle_Init" />
    <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
  </LayoutTemplate>
  <ItemTemplate>
  </ItemTemplate>
</asp:ListView>

Et capturez une référence au contrôle à utiliser dans le code-behind (par exemple) dans l'événement DataBound du ListView:

private Literal litControlTitle;

protected void litControlTitle_Init(object sender, EventArgs e)
{
    litControlTitle = (Literal) sender;
}

protected void lv_DataBound(object sender, EventArgs e)
{
    litControlTitle.Text = "Title...";
}

Pour la boucle LV imbriquée:

void lvSecondLevel_LayoutCreated(object sender, EventArgs e)
{
    Literal litText = lvFirstLevel.FindControl("lvSecondLevel").FindControl("litText") as Literal;
    litMainMenuText.Text = "This is test";
}

Si vous avez besoin de la version VB, la voici

Dim litControl = CType(lv.FindControl("litControlTitle"), Literal)
litControl.Text = "your text"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top