Click here to Skip to main content
Click here to Skip to main content

Server Side Message Box in VB.NET and C#

, 22 Nov 2004
Rate this:
Please Sign up or sign in to vote.
A custom control - server side MessageBox in VB.NET and C#.

Introduction

Using JavaScript, you can access the dialog boxes while on page loading or after rendering the page content to the browser. There is no way to get a confirmation from the user while you process the functionality through JavaScript. To accomplish this, you have to go for a server side message box. Server side message box provides an option to get a confirmation from the user based on some functionality checking. After getting the confirmation from the user click, it processes the functionality based on user selection.

Message box displays a message in dialog box, waits for the user to click the button, and returns a value indicating which button the user clicked. Based on the user selection, the functionality to be performed can be executed.

While comparing with Windows applications, it’s a bit hard to implement this in web based solutions. At the same time, you can achieve this using JavaScript. JavaScript implementations having some limitations like inability to customize the buttons in a message box.

In this article, I am not explaining in much detail about my implementation. Here, I provide the code for implementation. Hope you understand the implementation with out any problems.

After implementing my DLL, you can get a message box like this:

Features of this box

  1. You can change the title text dynamically.
  2. You can change the title font dynamically.
  3. You can change the title font size dynamically.
  4. You can change the title font style dynamically.
  5. You can change the title font name dynamically.
  6. You can change the title back color dynamically.
  7. You can change the message text dynamically.
  8. You can change the message font dynamically.
  9. You can change the message font size dynamically.
  10. You can change the message font style dynamically.
  11. You can change the message font name dynamically.
  12. You can change the image dynamically.
  13. You can change the image border dynamically.
  14. You can change the image border color dynamically.
  15. You can change the message box back color dynamically.
  16. You can change the message box border color, size dynamically.
  17. You can dynamically change the desired buttons, Yes / No / Cancel / Ignore.
  18. You can also change the button forecolor, borders, style etc. dynamically.

When the message box is displayed, all the form controls are in the disabled state and vice versa. You can even drag the message box using a mouse.

DLL Implementation

Go to Solution Explorer, click Add References, and specify the path of the DLL file which has implementation of message box control class.

Follow these steps:

  1. At the top of your web form code behind, add the following line of code:

    VB.NET

    Imports NewMsgBoxAsp.Holool.Anwar.Web.Controls.UI

    C#

    Using NewMsgBoxAsp.Holool.Anwar.Web.Controls.UI;
  2. Create an instance of MessageBox class, to access its properties and methods:

    VB.NET

    Dim NBox As MessageBox = New MessageBox

    C#

    MessageBox NBox = New MessageBox();
  3. Drag an ASP Button on the web form and paste the following code:

    VB.NET

    Private Sub Button1_Click(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles Button1.Click
        NBox.SetEnabledAll(False, Me)
        NBox.MB_Button = 3
        NBox.MB_Top = 50
        NBox.MB_Left = 150
        NBox.MB_Width = 300
        NBox.MB_Height = 150
        NBox.MB_ButtonWidth = 60
        NBox.MB_IDYes = "yesno"
        NBox.MB_IDNo = "yesno"
        NBox.MB_IDCancel = "yesno"
        NBox.MB_ButtonYesText = "Yes"
        NBox.MB_ButtonNoText = "No"
        NBox.MB_ButtonCancelText = "Cancel"
        NBox.MB_Title = "Uer Custom Title Here... .."
        NBox.MB_Message = "Uer Custom Message Here... .."
        NBox.MB_MessageFontSize = 10
        NBox.MB_Image = "d.gif"
    
        NBox.MB_TitleBarColor = System.Drawing.Color.Orange
        NBox.MB_BoxColor = System.Drawing.Color.LightSeaGreen
        NBox.MB_BoxShadowColor = System.Drawing.Color.LightSalmon
        NBox.MB_TitleFontColor = System.Drawing.Color.Maroon
        NBox.MB_TitleFontName = "Ariel"
        NBox.MB_TitleFontBold = True
        NBox.MB_ImageBorderSize = 5
        NBox.MB_ImageBorderColor = System.Drawing.Color.Red
        NBox.MB_ImageBorderStyle = BorderStyle.Double
    
        NBox.MB_ButtonBackColor = System.Drawing.Color.Green
        NBox.MB_ButtonBorderColor = System.Drawing.Color.Red
        NBox.MB_ButtonBorderWidth = 4
        NBox.MB_ButtonBorderStyle = BorderStyle.Double
        NBox.MB_ButtonForeColor = System.Drawing.Color.Yellow
        NBox.MB_ButtonFontBold = True
        NBox.MB_BorderWidth = 5
        NBox.MB_BorderStyle = BorderStyle.Double
        NBox.MB_BorderColor = System.Drawing.Color.Honeydew
        Panel1.Controls.Add(NBox)
    End Sub

    C#

    private void Button1_Click(object sender, System.EventArgs e)
    {
        MessageBox myBox = new MessageBox();
        myBox.MB_Button = 3;
        myBox.MB_Width = 300;
        myBox.MB_Height = 150;
        myBox.MB_ButtonWidth = 60;
        myBox.MB_IDYes = "yesno";
        myBox.MB_IDNo = "yesno";
        myBox.MB_IDCancel = "yesno";
        myBox.MB_ButtonYesText = "Yes";
        myBox.MB_ButtonNoText = "No";
        myBox.MB_ButtonCancelText = "Cancel";
        myBox.MB_Title = "Uer Custom Title Here... ..";
        myBox.MB_Message = "Uer Custom Message Here... ..";
        myBox.MB_MessageFontSize = 10;
        myBox.MB_Image = "d.gif";
        myBox.MB_TitleBarColor = System.Drawing.Color.Orange;
        myBox.MB_BoxColor = System.Drawing.Color.LightSeaGreen;
        myBox.MB_BoxShadowColor = System.Drawing.Color.Violet;
        myBox.MB_TitleFontColor = System.Drawing.Color.Red;
        myBox.MB_TitleFontName = "Ariel";
        myBox.MB_ImageBorderSize = 5;
        myBox.MB_ImageBorderColor = System.Drawing.Color.DarkGreen;
        myBox.MB_ImageBorderStyle = BorderStyle.Double;
        myBox.MB_ButtonBackColor = System.Drawing.Color.Green;
        myBox.MB_ButtonBorderColor = System.Drawing.Color.Red;
        myBox.MB_ButtonBorderWidth = 4;
        myBox.MB_ButtonBorderStyle = BorderStyle.Double;
        myBox.MB_ButtonForeColor = System.Drawing.Color.Yellow;
        myBox.MB_BorderWidth = 5;
        myBox.MB_BorderStyle = BorderStyle.Double;
        myBox.MB_BorderColor = System.Drawing.Color.Honeydew;
        Panel1.Controls.Add(myBox);
    }

    So, when you click the button, the message box is displayed. But wait, still there is something to be done. Normally, in a Windows message box, the user can drag the message to any location of the screen. This is not possible with a server control. To achieve this functionality, I have designed one more custom control called as WebControlDraggerS.

    This invisible control can be used to drag the message box to any part of the screen at run time, like any other classic Windows messagebox.

    To use this control, right click on the toolbox, select add/new items, and specify the path of WebControlDraggerS DLL file. Once the control is on the toolbox, drag it on the form.

    Next, paste the following methods, ControlToDrag and Panel1_PreRender to code behind:

    Private Sub ControlToDrag(ByVal ctrl As Control)
     ' Define the JavaScript function for the specified control.
     Dim focusScript As String = " <SCRIPT language="'javascript'">" & _
        "Drag.init(document.getElementById('" + ctrl.ClientID & "'), _
        null, null, null, null, null, false, false); </SCRIPT>"
    
     ' Add the JavaScript code to the page.
     Page.RegisterStartupScript("FocusScript", focusScript)
    End Sub
    
    Private Sub Panel1_PreRender(ByVal sender As Object, _
           ByVal e As System.EventArgs) Handles Panel1.PreRender
     ControlToDrag(Panel1)
    End Sub

    Here, Panel1 is the ID of the ASP Panel placed on the form, which acts as a container to the custom web message box.

    Finally, add the following subroutine in your code behind, and call this in Page_Load event.

    VB.NET

    Private Sub CheckYesNo()
       If Request.Form("yesno") = "Yes" Then
            Response.Write("Button - Yes - Selected")
       ElseIf Request.Form("yesno") = "No" Then
            Response.Write("Button - No - Selected")
       ElseIf Request.Form("yesno") = "Cancel" Then
            Response.Write("Button - Cancel - Selected")
       End If
       NBox.SetEnabledAll(True, Me)
    End Sub

    C#

    public void CheckYesNo()
    {
        if (this.Request.Form["yesno"]== "Yes")
            Response.Write("Button - Yes - Selected");
        elseif (this.Request.Form["yesno"]== "No")
            Response.Write("Button - No - Selected");
        elseif (this.Request.Form["yesno"]=="Cancel")
            Response.Write("Button - Cancel - Selected");
        myBox.SetEnabledAll(true,this);
    }

Enhancements:

This MessageBox control works similar to the classic Windows MsgBox, and contains all the properties and events. It does not require any enhancements. In fact, the message box is developed using VB.NET (Web based). Now I am trying to convert it to C#, to further increase its performance and integrate DLLs into a single one.

Using The Code (DLLs)

I am also placing a full demo ZIP of this MessageBox functionality in a separate ZIP file: a fully featured Data Entry Form which interacts with a Service Layer feature to perform DML transactions. The two DLLs can be found in the same project folder (NewMessageBox.dll and WebControlDragerS.dll). You can check the use of message box to add and save a new record. You can just explode the ZIP file and use it.

Different Views of Server Side Message Box

Happy Instantiating..!!

Rock’n Roll….. ………………

By Bye.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Anwar Hussain is a Software Developer (Project Leader) presently working for an MNC based in Bangalore, Karnataka ( I N D I A ). He is an Microsoft Certified Application Developer ( .NET ) and presently working on .NET Technology.
 
If you want to have a drink or go to a rock concert with me please feel free to contact... ..!!

Comments and Discussions

 
Questionalert box PinmemberMember 1084386716-Nov-14 20:18 
QuestionI'm not able to run in C# plz Help! PinmemberFaisalfarough9-Jul-11 4:15 
Questionwhen used with masterpage....Request.Form not returning any value....help Pinmemberzubair.irfan7-Jul-11 20:01 
GeneralMessage box not working with Master page Pinmemberfaruk rangooni14-Apr-11 2:43 
GeneralMy vote of 1 PinmemberSyed Javed26-May-10 0:34 
GeneralMessagebox not working in 2 pages alone. Please help PinmemberSrinivasanT20-Nov-09 21:49 
GeneralIt Works PinmemberOke Umar31-Jul-09 4:25 
GeneralOnly Half Msgbox window is appear on client machine Pinmembernehamayank25-Nov-08 1:04 
GeneralCompile error Pinmembermphatd6-Sep-08 8:24 
GeneralIt works PinmemberBernardoR2-Sep-08 19:56 
Generalwidth of newMessage Pinmember_adrian29-Nov-07 17:54 
Generalbutton causes validation event... Pinmembercometburn00729-Aug-07 18:47 
Generalpls give explain about this line Pinmembervenkat_kts1-Aug-07 2:08 
GeneralThank You PinmemberMember #280122218-Mar-07 6:29 
Generalwarning when build solution PinmemberMember #329427114-Jan-07 22:33 
GeneralProblems With MessageBox PinmemberMatt Clark9-Jan-07 8:39 
GeneralDisabling HTML Controls Pinmembercode4leo18-Dec-06 23:14 
GeneralClick button got error PinmemberFrank Babz17-Jun-06 7:37 
GeneralRe: Click button got error PinmemberFrank Babz17-Jun-06 7:49 
GeneralDragging not working. Pinmembercheeryperson28-Apr-06 23:00 
GeneralRe: Dragging not working. PinmemberLakshmiDurai7-Nov-06 18:50 
GeneralA Very Good Article PinmemberHimaBindu Veeramachaneni2-Mar-06 2:21 
GeneralFind control requires that controls have unique ID's Pinmembercai107518-Jan-06 18:29 
GeneralRe: Find control requires that controls have unique ID's Pinmembercathyxyzhang5-Feb-06 16:18 
GeneralNot working in C# PinmemberDeephaaa18-Jan-06 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 23 Nov 2004
Article Copyright 2004 by Anwar Hussain (Technical Specialist)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid