Last post May 18, 2017 12:20 PM by markfitzme
May 18, 2017 10:24 AM|RelapseIntoVoid|LINK
I have a simple ASP.NET Web forms website. It consists of a page and a custom user control that lives on the page. The page also have a textbox.
The user control consists of two boxes (one red, one blue - both ImageButtons) which both hook on to the same event handler (by OnCommand, CommandName and CommandArgument). This event handler updates an text file with the CommandArgument from the event. The
user control also has a public property where I store the current value (from the text file). This property is set in the page's (default.aspx) Page Load event.
Now, the page (default.aspx) is at all times supposed to fetch the value that is stored in the text file and display it. I do this in the Page Load event of the page. Like I said, I also set the user control's public property here with the same value.
Let's say that the value in the text file is "Red". I load the page and sure enough - "Red" is shown on the page. Now, if I click on the blue box (lives in the user control), "Red" is still on the page. It should be "Blue". It seems to me that the page's
Page Load - load before the text file gets updated.
How can I get around this?
May 18, 2017 12:20 PM|markfitzme|LINK
You are correct. The event handler is going to fire after load occurs. The page doesn't know the value has changed.
The way around this problem is to notify the page. You can your own custom event on the user control that fires after you have updated the file, and after you have updated the property. Then have the page subscribe to that and re-set the text. So initially
it would be set to Red. After the button event fires, you update the property to Blue, the control fires the custom event you created, then the page handles the event from the user control and updates the textbox to the controls property setting the textbox