Install MyLabel.Text in Repeater HeaderTemplate

Every sample I found is to write a function outside of my OnLoad page to do this, but I'm curious if there is a more concise way to do this. I have a shortcut inside the HeaderTemplate and I just want to set the label text to a string. I can do the following if the label is outside the repeater:

Month.Text = Enum.GetName(typeof(Month), Convert.ToInt16(MonthList.SelectedValue));

Is there a concise way to do this?

+3
source share
3 answers

Try the following inside the header template:

<asp:Label ID="Month" runat="server" Text='<%# (Month)Convert.ToInt16(MonthList.SelectedValue) %>' />
+2
source

It would be better if you used the DataBinding event.

ASPX markup:

<asp:Repeater ID="repTest" runat="server">
    <HeaderTemplate>
        <asp:Label ID="lblHeader" runat="server" />
    </HeaderTemplate>
</asp:Repeater>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    repTest.ItemDataBound += new RepeaterItemEventHandler(repTest_ItemDataBound);

    int[] testData = { 1, 2, 3, 4, 5, 6, 7, 8 };
    repTest.DataSource = testData;
    repTest.DataBind();
}

void repTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        Label lblHeader = e.Item.FindControl("lblHeader") as Label;
        if (lblHeader != null)
        {
            lblHeader.Text = "Something";
        }
    }
}

There you go :)

+9
source

100%, , , :

var myLabel = MyRepeater.Controls[0].Controls[0].FindControl("MyLabel") as Label;
myLabel.Text = "Hello World";

, , [0].

+4
source

Source: https://habr.com/ru/post/1708779/


All Articles