Click here to Skip to main content
16,016,814 members
Articles / Web Development / ASP.NET
Article

Server Side Message Box in VB.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.44/5 (18 votes)
22 Nov 20044 min read 277.4K   4.3K   69   43
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:

Image 1

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

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

    C#

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

    VB.NET

    VB
    Dim NBox As MessageBox = New MessageBox

    C#

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

    VB.NET

    VB
    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#

    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:

    VB
    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

    VB
    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#

    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

Image 2

Image 3

Image 4

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


Written By
Web Developer
India India
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 Pin
Member 1084386716-Nov-14 19:18
Member 1084386716-Nov-14 19:18 
QuestionI'm not able to run in C# plz Help! Pin
Faisalfarough9-Jul-11 3:15
Faisalfarough9-Jul-11 3:15 
Questionwhen used with masterpage....Request.Form not returning any value....help Pin
zubair.irfan7-Jul-11 19:01
zubair.irfan7-Jul-11 19:01 
GeneralMessage box not working with Master page Pin
faruk rangooni14-Apr-11 1:43
faruk rangooni14-Apr-11 1:43 
GeneralMy vote of 1 Pin
Syed J Hashmi25-May-10 23:34
Syed J Hashmi25-May-10 23:34 
Where is the source of DLL
GeneralMessagebox not working in 2 pages alone. Please help Pin
SrinivasanT20-Nov-09 20:49
SrinivasanT20-Nov-09 20:49 
GeneralIt Works Pin
Oke Umar31-Jul-09 3:25
Oke Umar31-Jul-09 3:25 
GeneralOnly Half Msgbox window is appear on client machine Pin
nehamayank25-Nov-08 0:04
nehamayank25-Nov-08 0:04 
GeneralCompile error Pin
mphatd6-Sep-08 7:24
mphatd6-Sep-08 7:24 
GeneralIt works Pin
BernardoR2-Sep-08 18:56
BernardoR2-Sep-08 18:56 
Generalwidth of newMessage Pin
_adrian29-Nov-07 16:54
_adrian29-Nov-07 16:54 
Generalbutton causes validation event... Pin
cometburn00729-Aug-07 17:47
cometburn00729-Aug-07 17:47 
Generalpls give explain about this line Pin
venkat_kts1-Aug-07 1:08
venkat_kts1-Aug-07 1:08 
GeneralThank You Pin
Rahman Mohammadi18-Mar-07 5:29
Rahman Mohammadi18-Mar-07 5:29 
Generalwarning when build solution Pin
Member 329427114-Jan-07 21:33
Member 329427114-Jan-07 21:33 
GeneralProblems With MessageBox Pin
Matt Clark9-Jan-07 7:39
Matt Clark9-Jan-07 7:39 
GeneralDisabling HTML Controls Pin
code4leo18-Dec-06 22:14
code4leo18-Dec-06 22:14 
GeneralClick button got error Pin
Frank Babz17-Jun-06 6:37
Frank Babz17-Jun-06 6:37 
GeneralRe: Click button got error Pin
Frank Babz17-Jun-06 6:49
Frank Babz17-Jun-06 6:49 
GeneralDragging not working. Pin
cheeryperson28-Apr-06 22:00
cheeryperson28-Apr-06 22:00 
GeneralRe: Dragging not working. Pin
LakshmiDurai7-Nov-06 17:50
LakshmiDurai7-Nov-06 17:50 
GeneralA Very Good Article Pin
HimaBindu Vejella2-Mar-06 1:21
HimaBindu Vejella2-Mar-06 1:21 
GeneralFind control requires that controls have unique ID's Pin
cai107518-Jan-06 17:29
cai107518-Jan-06 17:29 
GeneralRe: Find control requires that controls have unique ID's Pin
Ca20055-Feb-06 15:18
Ca20055-Feb-06 15:18 
GeneralNot working in C# Pin
Deephaaa17-Jan-06 23:13
Deephaaa17-Jan-06 23:13 

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

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