Asp.net imitates winform's MessageBox

Source: Internet
Author: User
Copyright: Original Works are allowed to be reproduced. During reprinting, be sure to mark them as hyperlinks. Article Original source, author information, and this statement. Otherwise, legal liability will be held. Http://www.cnblogs.com/jintan/archive/2008/10/25/1319308.html

In the morning, I accidentally saw the "Confirm operation by VAR JS = confirm (" in the garden? "); The article" technical difficulties "gives me the first feeling: Oh, there were so many people who wanted to use MessageBox in webform as convenient as calling in winform. I have always thought that few people are paying attention to such a small detail. A year ago, I decided to create a server-side control just because I had the same idea and make it easy for future projects to reuse.

First take a look At MessageBox. Show () in winform ():

Dialogresult result = MessageBox. Show ( " Are you sure you want to delete it? " , " Prompt " , Messageboxbuttons. yesno, messageboxicon. information );
If (Result = Dialogresult. Yes)
{
// Corresponding logic processing
}
Else
{
// Corresponding logic processing
}

 

When MessageBox. show is called in winform, the main thread will be suspended until the user confirms that the followingCodeIn JavaScript, mongown. Confirm () can also implement this function,
If (window. Confirm ("are you sure you want to delete it? "))
{
.....
}

To achieve this on the server side, you only need to use registerclientscriptblock () to register the client script.
Now we have two more problems:
When the client clicks "OK" or "cancel", how does the server receive the message? Some people say that using _ dopostback ('','') is correct, but we cannot write such code manually on every page, right? You have to encapsulate it into a custom control. You only need to implement the ipostbackeventhandler interface.
After the client shows the confirm () message dialog box, it only blocks the main thread of the current browser of the client and does not block the main thread of the server! This is also the biggest problem we have encountered. Since we cannot block the main thread on the server side, can we provide another alternative? Just like the following C # code:

Protected   Void Button#click ( Object Sender, eventargs E)
{
// Msgbox is our encapsulated Server Control
Msgbox. Show ( " Need to delete it? " , (Msender, ARG) =>
{
If (Arg. Result = Dialogresult. Yes)
{
Label1.text =   " OK, the logic result of the server execution " ;
}
Else
{
Label1.text =   " No. logic result of server execution " ;
}
}
);

}

Prototype of the show () method:

///   <Summary>
/// The client displays the confirm message box.
///   </Summary>
///   <Param name = "message"> Message Content </Param>
///   <Param name = "message"> The delegate for post-return processing. </Param>
Public   Void Show ( String Message, message objeventhandler)

 

In fact, a delegate is passed in the 2nd parameters of show, and its return value is included in Arg. To put it bluntly, it is a callback method, this method is triggered only when the client clicks "OK" or "cancel. In this way, when the client clicks confirm or cancel, the result value of the click is returned to the server, and the server determines and performs corresponding processing. This raises another question: Is the delegate stored in viewstate or session ?? If it is stored in viewstate, serialization and deserialization are involved. The delegate can be serialized, but the referenced objects cannot be serialized. target may be a page object that cannot be serialized), and the efficiency of many objects in viewstate is indeed a little poor. What if session is used? Although serialization can be avoided, the session key name is involved, because it is necessary to ensure that each independent control key name on the page cannot be repeated, otherwise it will lead to many problems.

 

Public   Delegate   Void Message ( Object Sender, messageboxeventhandler E );
// Delegate for callback Processing
Private Message messageboxcallback
{
Get
{
Return   This . Page. session [getsessionkey ( " Messageboxcallback " )] As Message;
}
Set
{
If (Value =   Null )
{
This . Page. session. Remove (getsessionkey ( " Messageboxcallback " ));
}
Else
{
This . Page. session [getsessionkey ( " Messageboxcallback " )] = Value;
}
}
}

// Generates a unique session key for the control.
Private   String Getsessionkey ( String Key)
{
Return Key +   This . Page. GetType (). fullname +   This . Clientid;
}

 

In this way, the main problem is basically solved. I hope you will give more comments.

Updated:, modified the session key acquisition method and assigned different keys for different pages

Code
// Delegate for callback Processing
Private Message messageboxcallback
{
Get
{
Return   This . Page. session [getsessionkey ( " Messageboxcallback " )] As Message;
}
Set
{
If (Value =   Null )
{
This . Page. session. Remove (getsessionkey ( " Messageboxcallback " ));
}
Else
{
This . Page. session [getsessionkey ( " Messageboxcallback " )] = Value;
}
}
}

Private   String Pageidentify
{
Get
{
If (Viewstate [ " Pageidentify " ] = Null )
{
Viewstate [ " Pageidentify " ] = System. guid. newguid (). tostring ();
}
Return Viewstate [ " Pageidentify " ]. Tostring ();
}
}

// Generates a unique session key for the control.
Private   String Getsessionkey ( String Key)
{
Return Key + Pageidentify +   This . Clientid;
}

Demo: http://www.webcontrols.com.cn/msg/
Source code download: http://files.cnblogs.com/jintan/TestMessageBox.rar

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.