tags:

views:

176

answers:

2

I'm playing around with a form utilizing a repeater that I've been working on, and I'm currently trying to write an event handler that will get the value of certain controls inside of a repeater after the form containing the repeater has been submitted. I assumed that something similar to this would work if I wanted to retrieve the label from the first repeater item, then set the text of it to the text of another label:

protected void SubmitMessage_Click(object sender, EventArgs e)
{
    Label MyLabel = (Label)MyRepeater.Items[0].FindControl("MessageID");
    MyLabel2.Text = MyLabel.Text;
}

Using this however, it seems that MyLabel.Text is empty. When testing if MyLabel is null after setting it, the result is false, so I'm trying to figure out any reason why MyLabel wouldn't have the same contents of that in the repeater item that I'm trying to set it to. MyLabel2 is already declared at the top of the class. Thanks in advance.

EDIT: The text of the label in the .aspx file is not being databound inside the text attribute, but rather in the container of the label itself. I'll try the other method on Tuesday morning, as soon as I can get back inside my code, on Benjamin's suggestion.

A: 

I'm not sure about this but when the event is executed the repeater might not have yet repopulated. Since the call to the method is "ByVal" (VB speak) and not ByRef the value might get passed but the original object is not yet populated... Just a thought...

Sani Huttunen
Late night and a few beers so don't judge me too hard... ;)
Sani Huttunen
+2  A: 
benjamin
DataBinding would already have happened as it's after the Page_Load (or other) and before the next Page_load (or other)...?
Greg B
The data is in the repeater and shows on the page but if the Label Text attribute is not set explicitly and they are placing the data in the body of the label then the label itself is never truly bound.Labels are not meant to be set in such a way and in this instance you can see why.
benjamin
But how does setting the data inside the label (not inside the text attribute) done client-side rather than server side; doesn't that imply that javascript or the like would have to be generated for the browser to do this? I'm not making assertions, as I don't have any of the code in front of me, but as I understand it, all inline script is processed server side.
lush
That might be a good question because if you do some test cases you get wildly different results depending on the order of things you are trying to do. I don't have the answer to why it works the way it does I just know what it does and how to form it to get the results you want even if they were not the results you would logically expect. If you don't use inline script in the body of the label the body text overwrites the Text attribute text, but inline scripts throw those normal rules out the window.
benjamin
I edited my initial answer to clear up the confusion of client-side rendering and instead used the order of events that I believe is happening to cause the inconsistencies.
benjamin
Awesome, I can't wait to try this out. Thanks for the clarification.
lush