ASP. NET can also use the msgbox method.

Source: Internet
Author: User

Use VB6 or VB. NET developers will always use the msgbox function to display messages. net in the Development of ASP. net program, you will find that the msgbox function seems to be usable, And you will directly use msgbox to display messages.

 

Msgbox ("this is a message ")

 

In the vs development environment, the msgbox function can display messages normally. However, once the website is released, the msgbox function becomes invalid. Why? Msgbox is a servo language, and the client can only parse HTML and JavaScript through a browser. Therefore, msgbox functions cannot be used in ASP. NET programs and can only be used for debugging. In ASP. NET, the correct way to display messages on the client is to use the registerstartupscript method to output the alert function of JavaScript.

 

Scriptmanager. registerstartupscript (Me, me. GetType (), "alert", "alert ('this is message')", true)
 
Or

Me. clientscript. registerstartupscript (Me. GetType (), "alert", "alert ('this is message')", true)

 

Is it too much trouble to display messages in ASP. NET? Can you use msgbox to display messages in ASP. NET? In fact, the method is very simple, just write a msgbox method by yourself, and you can extend the msgbox you used to display messages. We wrote the msgbox method in basepage. This msgbox method also processes the section of the message body containing "single quotes" and "line breaks" and converts these special strings into corresponding JavaScript escape characters.

 

''' <Summary>
''' Prompt message is displayed.
''' </Summary>
''' <Param name = "message"> message body. </Param>
Public sub msgbox (byval message as string)
Dim sscript as string
Dim smessage as string

Smessage = strings. Replace (message, "'", "\'") 'process single quotes
Smessage = strings. Replace (smessage, vbnewline, "\ n ")'
Sscript = string. Format ("alert ('{0}');", smessage)
Scriptmanager. registerstartupscript (Me, me. GetType (), "alert", sscript, true)
End sub

 

All pages that inherit the basepage can use the msgbox method to display messages.

 

Protected sub button#click (byval sender as object, byval e as system. eventargs) handles button1.click
Dim smessage as string

Smessage = "alert message line feed test" & vbnewline & "test 'single quota '"
Msgbox (smessage)
End sub

 

 

Handler handler: processing the Javascript related function, attackers use the methods provided in the article "create your own scriptmanager to manage Terminal commands" to handle this problem, therefore, the msgbox method can be written into the tbscriptmanager class in this article.

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.