The style is lost after the JavaScript script page is added to the background for sending back

Source: Internet
Author: User
Development Environment v. S. 2008. Net 3.5

Symptom:
The work filling page contains the widget and a submit button.
In the script in the background, there is a button OnClick event
Event content
        
If the storage fails, a friendly message indicating that the storage is unsuccessful is displayed.
The same is true for success.

The script code is as follows:
     
 

This. Response. Write ("<script> alert ('storage failed! ') </Script> ");

After running, click "save failed". A dialog box is displayed, but the style is lost.

Cause:
When this. Response is used in the background, the script will be added to the back-to-customer stream before the Render method, and then the Page
When the page ends, call render () to generate a normal page stream and add it to the stream sent back to the client. What the browser receives is

<Script> alert ('Storage failed! ') </Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
</Head>
<Body>
</Body>
</Html>

Such a combination may sometimes cause style loss (in this case, the style may be lost due to different reasons) (the browser cannot parse it correctly)

Solution
In summary, it is to register the client code through the background.
The following is a simple example.
  

ClientScriptManager cs = Page. ClientScript;
Cs. RegisterClientScriptBlock (typeof (Object), "alert", "<Script> window. alert ('fault ticket submitted successfully, serial number:" + GetFaultInfo (). Fault_NO + "! ') </Script> ", True );

Note that the last True must be added when assigning values to the method. In this way, you can rewrite the script block with the same name on the page.
   ClientScriptManagerClass is used to manage client scripts and add them to Web applications.

In Shanghai

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.