views:

1318

answers:

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?

+2  A: 

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
first you need to MyRepeater.DataBind(), then you can access it's controls. I guess one Controls[0] is enough :)
balint
No idea why you were voted down, this worked perfectly for me.
lush
I just had to add it after my DataBind.
lush
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
Gotcha, I didn't completely understand how it worked until now.
lush
+2  A: 

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
How is writing an event handler for my purpose better than not?
lush
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
Great advice, thanks a lot.
lush
A: 

Try the following inside your header template:

<asp:Label ID="Month" runat="server" Text='<%# (Month)Convert.ToInt16(MonthList.SelectedValue) %>' />
Dave Cluderay
I get an error stating that my enum type couldn't be found.
lush
Wow! Bit harsh with the downvoting - you probably just needed an <%@ Import Namespace="Xyz" %>.
Dave Cluderay
Really sorry about that, you were completely right in the comments. I just needed to import.
lush
Great! Thanks for retrying it.
Dave Cluderay