Search code examples
asp.netmessageboxwebusercontrol

Popup message box in c#


I'm looking for a custom user control similar to http://www.how-to-asp.net/messagebox-control-aspnet/ but having the ability to be displayed as a popup. The message box should have the capability of being invoked from code behind in asp.net 4 with event hooks to bind the "ok" and "cancel" button.

I'm familiar with Ajax Toolkit and JQuery.

A reference and or sample in a similar direction would be very helpful.

Thanks!


Solution

  • In my experience, it's usually a sign of a bad design if you want to open something on the client side from the server side code behind. Are you sure that's what you need?

    But assuming you do, you can use the ModalPopupExtender from the Ajax Control Tookit. It can be opened from client or server side. Here's a sample:

    <ajaxToolkit:ModalPopupExtender ID="MPE" runat="server"
        TargetControlID="LinkButton1" ClientIdMode="Static"
        PopupControlID="Panel1" />
    

    The PopupControlID should be the ID of a panel that you want to appear as a popup. You can put buttons on that panel if you need to. From the code behind, it's as simple as this...

    MPE.Show();
    

    To show it from JavaScript (assuming jQuery), make sure you set the ClientIdMode to Static, then call it...

    $('#MPE').show();