ASP. NET submits the form after assigning a value to the TextBox that is used as the hidden domain. No response ?, Asp. nettextbox

Source: Internet
Author: User

ASP. NET submits the form after assigning a value to the TextBox that is used as the hidden domain. No response ?, Asp. nettextbox

Procedure:

Hide TextBox values on the page, and then trigger ASP. NET change event, called ASP. NET background method. After the call, execute the client script this. registerClientScriptBlock (DateTime. now. toString (), script );

Execute the script of the page. The script of the page assigns a value to the hidden TextBox, and then triggers the ASP. NET change event. At this time, the ASP. NET change event does not enter the background.

 

Solution:

Change this. RegisterClientScriptBlock (DateTime. Now. ToString (), script); to Page. RegisterStartupScript (DateTime. Now. ToString (), script );

 

Cause analysis:

Response. method 1: Use Response. write, this method will Write the JS Code at the top of the page (Before): System. web. httpContext. current. response. write ("method 2, using RegisterStartupScript, this method will embed JS Code at the bottom of the page, the last (Front) of the form ), applicable to JS Code to run after page controls are loaded: System. web. UI. page page = (System. web. UI. page) System. web. httpContext. current. handler; if (! Page. clientScript. isStartupScriptRegistered (page. getType (), "clientScript") page. clientScript. registerStartupScript (page. getType (), "clientScript", "method 3, use RegisterClientScriptBlock. In this method, JS Code is embedded at the top of the page and the Front (back) of the form ), applicable to the JS code to be executed before the control is loaded, similar to the above Response. write method: System. web. UI. page page = (System. web. UI. page) System. web. httpContext. current. handler; if (! Page. ClientScript. IsClientScriptBlockRegistered (page. GetType (), "clientScript") page. ClientScript. RegisterClientScriptBlock (page. GetType (), "clientScript ","
So what is the difference between method 2 and method 3? The main difference is that the RegisterStartupScript method embeds JavaScript at the bottom of the ASP. NET page, just before the closing element.

The RegisterClientScriptBlock method is used to embed JavaScript into the closely following the Enable element in the page. So what's the difference? As we will see, this is a big difference. Here is an example. The following is a method to focus on a text box on the page when the page is loaded to a browser.
-Use Visual Basic: Page. clientScript. registerStartupScript (Me. getType (), "Testing", _ "document. forms [0] ['textbox1']. focus (); ", True) because the text box on the page is generated when the browser runs to the bottom of the page and executes this small JavaScript code, and it has been placed in the page, therefore, this method runs normally.
However, if you do not follow the preceding method, write the following code (using the RegisterClientScriptBlock method): Page. clientScript. registerClientScriptBlock (Me. getType (), "Testing", _ "document. forms [0] ['textbox1']. the focus (); ", True) text box control will not get the focus, and a JavaScript error will be generated on the page because RegisterClientScriptBlock and RegisterStartupScript registered scripts have different positions, the two are also used in different scenarios: 1> the script registered by RegisterClientScriptBlock is executed before the DOM element is fully loaded, which leads, it cannot access almost all html elements on the page;
Therefore, RegisterClientScriptBlock is suitable for writing JavaScript Functions and warnings (the page is blank before the user clicks "OK"). 2> the scripts registered by RegisterStartupScript are also executed before the DOM element is not fully loaded, unlike RegisterClientScriptBlock, most DOM elements on the page can be accessed;
Therefore, RegisterStartupScript is not suitable for writing JavaScript Functions. When you call the function, the function is not loaded to the page. RegisterStartupScript can also be used to write warnings, even if you do not click the "OK" button, you can see the content of the page. The alarm about RegisterStartupScript writing (alert ), if </form> is followed by a JavaScript script (directly hardcoded on the page) that controls the page rendering style, before the user clicks the "OK" button in the warning box, the page may be displayed unexpectedly. When processing form data, if (string. isNullOrEmpty (txtInput. value. trim () {ClientScript. registerStartupScript (Page. getType (), "", "alert ('cannot be blank! '); ", True); return ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.