A small function: alert that displays information on both PAGE and updatepanel

Source: Internet
Author: User

If we want to display a prompt on the page at ordinary times, we usually adopt a method similar to the following:

Page. clientscript. registerclientscriptblock (typeof (this), "key1 ", " <SCRIPT type = \ " Text / Javascript \ " > Window. Alert (\ " Test \ " ); </SCRIPT> ", True );

However, ifCodePut it on a page that contains updatepanel, there may be problems, because updatepanel has processed the HTTP input and output, and it is not allowed to manually add additional content to the webpage, this usually gives an exception. Microsoft once provided a list of ASP. NET Server controls that are incompatible with updatepanel. Many of them are for this reason. However, Microsoft also provides another function to solve this problem, namely scriptmanager. registerclientscriptblock (). The Javascript script registered using this method does not conflict with updatepanel.

To sum up the two cases, write the following small functions: Public   Class Webmessagebox
{
Public   Static   Void Show ( String Infomation, Control)
{
If (Stringhelper. isnullorempty (infomation ))
{
Throw   New Argumentexception ( " No information is set for display " );
}

BoolIspage=(ControlIsPage );
BoolIsupdatepanel=(ControlIsUpdatepanel );

If(!Ispage&& !Isupdatepanel)
{
Throw NewArgumentexception ("Information can only be output to page or updatepanel.");
}

Bool Addscripttags =   True ;
String Script =   String . Format ( " Window. Alert ('{0 }'); " , Infomation );
String Key =   String . Format ( " Webmessagebox {0} " , Datetime. Now. tostring ());
Type registertype =   Typeof (Webmessagebox );

If(Ispage)
{
Page=ControlAsPage;
Page. clientscript. registerclientscriptblock (registertype, key, script, addscripttags );
}

If (isupdatepanel)
{< br> updatepanel = Control as updatepanel;
scriptmanager. registerclientscriptblock (updatepanel, registertype, key, script, addscripttags);
}< BR >}

Note:
this function can be called directly if a message is prompted on the webpage. Pay attention to the second parameter.
This is not a complete code. There is a stringhelper. isnullorempty () method that I wrote to replace string. isnullorempty.
the script key generation is relatively lazy and has not been modified without affecting the overall situation.
I have previously thought about putting the other two prompt boxes (confirm and prompt) on it. Later I found that the logic is not true and it cannot achieve the effect similar to winform. If (webmessagebox. show ("input name", webmessageboxtype. prompt "=" admin ") {}. Do it for the moment. You may customize a window to achieve this effect in the future.
it's just a small piece of code, with no technical skills... ^ _ ^

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.