Click to See Complete Forum and Search --> : One control updating another.


rliq
June 8th, 2008, 09:37 PM
I have a very simple question.

An ASP Web form with 3 controls. Label, ComboBox and Button. When the page loads the Label and ComboBox both contain the same text. This bit works ok.

I choose a new item from the ComboBox drop down and press the button. Which does a postback (as there is some server side stuff that needs to be done).

How can I get the Label to display the newly selected ComboBox item?

It seems as though my server code, reacting to the Button.OnClick() event, is executed AFTER the page (Label) is reloaded, so it does not get the new value until the next time around...

What is the correct way to do this? I'm getting a bit frustrated ;)

Alsvha
June 8th, 2008, 11:25 PM
You are correct that event handling comes after the PageLoad, so what you should (can) do is to wrap the loading code which initialize the label inside a check that the page is not a postback (there is a method on the Page object for this), so it only gets sets the first time the page loads.

Then in the buttons event handler, you'll set the new value for the label.

rliq
June 8th, 2008, 11:49 PM
Alsvha,

Thank you for confirming what I just ended up doing to get it working! I just didn't think it was the correct way, but it seems to work smoothly and the database accesses (in the background) only happen when required...