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... ^ _ ^