3

Every sample I've found of doing this consists of writing a function outside of my page's OnLoad in order to do this, but I'm curious if there's a more concise way to go about it. I have a Label inside of a HeaderTemplate, and I just want to set the text of the label 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 succinct way to do this?

Christopher Garcia
  • 2,536
  • 7
  • 30
  • 40

3 Answers3

10

It would be better if you did use 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 :)

Gromer
  • 9,861
  • 4
  • 34
  • 55
  • How is writing an event handler for my purpose better than not? – Christopher Garcia May 21 '09 at 16:29
  • There's something about chaining a bunch of Conters[number] together that seems sketchy to me. That would have to be changed if you restructured the page. If anything, write a method that does a search through all the controls to find the one you need so it wouldn't break if you wrapped your markup with a div or something else. – Gromer May 21 '09 at 17:29
4

I'm not 100% certain whether or not you need to wait for the Repeater to have data bound to it or not, but this is how you would access a control within it's header:

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

You should probably break that into multiple lines and check to make sure that there is an object at Controls[0].

Jason
  • 1,766
  • 2
  • 14
  • 24
  • first you need to MyRepeater.DataBind(), then you can access it's controls. I guess one Controls[0] is enough :) – balint May 20 '09 at 23:03
  • No idea why you were voted down, this worked perfectly for me. – Christopher Garcia May 21 '09 at 16:26
  • I just had to add it after my DataBind. – Christopher Garcia May 21 '09 at 16:27
  • If I had to guess, it's because I'm using Controls[0] which isn't the most robust way of doing things. It's better to use FindControl since this method depends on the controls staying in the same order which may not be the case in the future. – Jason May 21 '09 at 19:36
  • Gotcha, I didn't completely understand how it worked until now. – Christopher Garcia May 22 '09 at 16:04
  • Since the Text I'm dumping in comes from a variable and not a page-control, I had to use this method (as opposed to the selected answer). It works, but makes me nervous, giving the drill-down and magic-string-control-name-ref. – Michael Paulukonis Aug 08 '12 at 14:27
2

Try the following inside your header template:

<asp:Label ID="Month" runat="server" Text='<%# (Month)Convert.ToInt16(MonthList.SelectedValue) %>' />
Dave Cluderay
  • 7,268
  • 1
  • 29
  • 28