ASP.net management Postback problem (cannot read value entered by user!)

I wrote my own widget so that the user can select a location from the location database. This is my first ASP.net user control. Everything seemed to be working fine, but now there is a problem.

My control implements the RaisePostBackEvent function as follows:

public void RaisePostBackEvent(string eventArgument) { SelectedLocationId = eventArgument.Split('|')[0]; SelectedLocationDescription = eventArgument.Split('|')[1]; } 

I wrote a test page and included the following in my ASP code:

 <%= locationSelector.SelectedLocationId %> 

This worked fine.

However, in my web application, the following code does not work:

  protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) Response.Write(locationSelector.SelectedLocationId); // SelectedLocationId is null here!!! } 

When I run this code in the debugger, I see that my "Page Load" event fires before the Post Back event! Therefore, data is not read after postback. I know that using an MS text field, text is available on the Load page, so I think I should do something wrong.

How can I find out the location that the user chose when the page load event was triggered? To clarify, I refer to the download page of the web application page.

+4
source share
1 answer

You set the SelectedLocationId to the postback event and at the same time trying to get its value on the first load. SelectedLocationId will have a null value.

Try:

 protected void Page_Load(object sender, EventArgs e) { if (locationSelector != null) Response.Write(locationSelector.SelectedLocationId); } 
+3
source

Source: https://habr.com/ru/post/1339035/


All Articles