简体   繁体   中英

ASP.NET Accessing AJAX created controls on postBack

I am generating 2 dropdown boxes and a CheckBoxList control at runtime using AJAX callbacks to a web service (.asmx file). The service creates the two Dropdowns and CheckBoxList on the server and returns the rendered html as a string which is injected into the web page at runtime, using javascript functions.

During postBack I want to obtain the values of the two dropdown boxes and also determine which (if any) of the checkboxes have been ticked.

Am I right in thinking that the HTML that is injected into the page at runtime is not sent back to the server during postback? If this is the case what would be the most sensible way of obtaining my values?

I'm thinking that the best way to obtain the values of the dynamic controls will be to use javascript to read the values and write them to a hidden field that's part of the page class, just before the postback.

If there is a better way to do this then please share!

One method of retrieving your values during a postback is by saving values in a hidden field which as the element name suggest is invisible on the page -

<asp:HiddenField id="countrySelected" value="" />

In your javascript before the post back you can populate your required information -

document.getElementById('countrySelected').value = 'USA';

In your codebehind you could then select the value to be used -

countrySelected.value

Since the webservice creates the html which is later injected in the page they are not server controls and therefore, the server has no way of knowing they exist. The Hidden html element seems to me the way to go on this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM