Click here to Skip to main content
Licence CPOL
First Posted 23 Aug 2005
Views 134,010
Bookmarked 22 times

PopUp Window With Different Buttons ( Windows Message Box )

PopUp Window With Different Buttons ( Windows Message Box )

     PopUp Window In ASP . NET

 

Introduction

This article describes an ASP.NET popup control. It is designed for use in a web page.

A very important feature of this control is, that it can be used on most of the current browsers. You can also use HTML in lot of control properties, so you can get popup with icon or anything you want. The below figure shows the message box with different buttons,

 

Sample screenshot

 

( Message Box with OK Button )

 

Sample screenshot

 

( Message Box with Yes No Buttons )

 

                                                

Sample screenshot

 

( Message Box with Ok Cancel Buttons )

 

Sample screenshot

 

( Message Box with Yes No Cancel Buttons )

Actions

The control has one event, Cmd_Click (Button in popup was clicked) and it contains four Buttons Namely Ok, Cancel, Yes and No. The buttons are displayed according to your requirement. I mean, if you want ‘Ok’ Button in the Message Box, you can pass 0 ( zero ) through session variable, and if you want ‘Ok – Cancel’ Buttons in the Message Box, you can pass 1  ( one ) through session variable and so on.

You can observe, the window title ( IE title), image, Custom Text are different in every message box. All these values can be changed dynamically using session variables. Even the number of buttons can be changed as per the requirement.

Using this control

This Control is actually a ASPX  Popup control, which uses sessions. It receives values through sessions from the client page and accordingly, it is displayed. To use the message box just include this aspx page in your web application and pass session values from the client web page as shown below,

 

 

Session("wTitle") = "Designed By Anwar Hussain Shaik"

Session("lblMsgBox") = "Hello, This is Ok Only"

Session("intCapCode") = "0"

Session("imgUrl") = file:///E:\Resources\loginImages\connected_multiple.jpg

 

Here wTitle     : is title if Explorer.

     lblMsgBox  : is the custom text to display.

     intCapCode : is the desired button you want to display on the MsgBox.

     Imgurl     : is the image Url to display image.

 

Note : please note that all the session variables are case sensitive.   

 

 

 

You can write the above block code on a button in the client we page and following is the code to display the popup or message box,

 

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

' Function To Display PopUp Window

    Private Sub subDisplayPopUp()

        Dim strJScript As String

        strJScript = "<script language=javascript>"

        strJScript += "window.open('CustomMsgBox.aspx',null,'height=150,  width=430,status= no, resizable= no, scrollbars=no, toolbar=no,location=center,menubar=no, top=100, left=300');"

        strJScript += "</script>"

        Response.Write(strJScript)

 

  ' Call Function To Display PopUp Window

        subDisplayPopUp()

    End Sub

End Sub

 

This is the code for the subroutine used in the above code.

 

' Function To Display PopUp Window

    Private Sub subDisplayPopUp()

        Dim strJScript As String

        strJScript = "<script language=javascript>"

        strJScript += "window.open('CustomMsgBox.aspx',null,'height=150, width=430,status= no, resizable= no, scrollbars=no, toolbar=no,location=center,menubar=no, top=100, left=300');"

        strJScript += "</script>"

        Response.Write(strJScript)

    End Sub

 

 

Finally, in your client page, add the following script, to get the values returned by the message box.

 

<script language="javascript">

            function first(MsgBoxRetval)

            {

              document.Form1.txtReturnResult.value = MsgBoxRetval;

            }

</script>

 

Here ‘txtReturnResult ’ is the name of textbox where I can the returned Value.

The client Web Page Looks something like This,

 

 

Sample screenshot

 

 

This Is a demo of Message Box Control, with four buttons as shown in the figure. You can see the returned value in the text box as shown.

 

 

 

Internal Working Of The control

At page load of popup window you can find the following code,

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Put user code to initialize the page here

 

        Response.Write("<title>" + Session("wTitle") + "</title>")

 

        Me.imgMsgBox.ImageUrl = Session("imgUrl")

 

        Me.lblMsgBox.Text = Session("lblMsgBox")

 

        ' Call function to Display Push Buttons as Needed

        funMsgBoxButtons(Session("intCapCode"))

    End Sub

See how Sessions values are used to assign values to the controls on the popup / message box window. This function receives an integer value forwarded by the client page, and accordingly, buttons are displayed on the message Box.

The code for the function funMsgBoxButtons() is shown below,

' Function which displays Ok,Yes,No,Cancel Captions on the Buttons

    Public Function funMsgBoxButtons(ByVal intCapCode As Integer)

 

        ' Call Function to Display All Buttons, then Hide/Unhide them Accordingly

        funVisibleButtons()

 

        Select Case intCapCode

            Case 0

                cmd1.Text = "Ok"

                cmd2.Visible = False

                cmd3.Visible = False

            Case 1

                cmd1.Text = "Ok"

                cmd2.Text = "Cancel"

                cmd3.Visible = False

            Case 2

                cmd1.Text = "Yes"

                cmd2.Text = "No"

                cmd3.Text = "Cancel"

            Case 3

                cmd1.Text = "Yes"

                cmd2.Text = "No"

                cmd3.Visible = False

        End Select

    End Function

What Happens when U click on the Ok – Cancel – Yes – No Buttons :

When U click on the Message Box buttons, the respective code is executed, There are three buttons on the Message Box,

' When the caption is Ok or Yes

    Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

        If cmd1.Text = "Ok" Then

           

            strReturnVal = 1

        ElseIf cmd1.Text = "Yes" Then

           

            strReturnVal = 2

        End If

 

        ' This Closes the Message Box

        funCloseMsgBox()

    End Sub

 

    ' When the caption is Cancel or No

    Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click

        If cmd2.Text = "Cancel" Then

           

            strReturnVal = 4

        ElseIf cmd2.Text = "No" Then

            strReturnVal = 3

        End If

 

        ' This Closes the Message Box

        funCloseMsgBox()

    End Sub

 

    ' When the caption is Cancel

    Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click

        If cmd3.Text = "Cancel" Then

           

            strReturnVal = 4

        End If

 

        ' This Closes the Message Box

        funCloseMsgBox()

    End Sub

 

    ' This Closes the Message Box and Returns the ... ... value

    Private Sub funCloseMsgBox()

        Response.Write("<script language=javascript>opener.first('" + strReturnVal + "');window.close();</script>")

    End Sub

 

Disadvantages Of this Control :

 

It runs at server side.  Actually, when you press the button, the popup windows or web page is requested from the server.

 

Future Enhancments:

 

This Control is a popup window. One can try it to convert it into a DLL ( Assembly ), which can be used just with a reference.

 

 

 

 

By Bye.

 

Shaik Anwar Hussain M. Tech (IT)

Software Engineer,

Holool India Ltd.,

 

E-mail : s_anwar786@rediffmail.com

 

 

 

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anwar Hussain (Technical Specialist)

Web Developer

India India

Member

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... ..!!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberSyed Javed17:19 4 Jul '11  
QuestionInput string was not in a correct format. Pinmembervyasv22:18 28 Aug '08  
Questionplease help PinmemberMember 134440822:38 11 Mar '08  
QuestionPopUp Window With Different Buttons PinmemberMember 13444089:50 11 Mar '08  
Generalsubect [modified] Pinmemberrobocato23:12 24 Apr '07  
GeneralVery Nice Article Pinmembersukanya lamu1:05 20 Dec '06  
GeneralReally a very good article, keep it up..!! PinsussAnonymous17:50 16 Oct '05  
GeneralGood PinsussAnonymous0:27 27 Sep '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 24 Aug 2005
Article Copyright 2005 by Anwar Hussain (Technical Specialist)
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid