Click here to Skip to main content
15,886,798 members
Articles / Programming Languages / C#
Article

PopUp Window With Different Buttons ( Windows Message Box )

Rate me:
Please Sign up or sign in to vote.
2.34/5 (15 votes)
23 Aug 2005CPOL6 min read 162.1K   1.6K   21   8
PopUp Window With Different Buttons ( Windows Message Box )

     PopUp Window In ASP . NET<o:p>

<o:p> 

Introduction<o:p>

This article describes an ASP.NET popup control. It is designed for use in a web page. <o:p>

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,<o:p>

<o:p> 

<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"><o:lock aspectratio="t" v:ext="edit"><o:p>

Sample screenshot

 

( Message Box with OK Button )

 

<o:p>Sample screenshot

<o:p> 

<o:p>

( Message Box with Yes No Buttons )<o:p>

<o:p> 

<o:p>                                                

Sample screenshot

<o:p> 

<o:p>

( Message Box with Ok Cancel Buttons )

 

<o:p>Sample screenshot

<o:p> 

<o:p>

( Message Box with Yes No Cancel Buttons )<o:p>

Actions<o:p>

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.<o:p>

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. <o:p>

Using this control<o:p>

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,<o:p>

<o:p> 

<o:p> 

Session("wTitle") = "Designed By Anwar Hussain Shaik"<o:p>

Session("lblMsgBox") = "Hello, This is Ok Only"<o:p>

Session("intCapCode") = "0"<o:p>

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

<o:p> 

Here wTitle     : is title if Explorer.<o:p>

     lblMsgBox  : is the custom text to display.<o:p>

     intCapCode : is the desired button you want to display on the MsgBox.<o:p>

     Imgurl     : is the image Url to display image.<o:p>

<o:p> 

Note : please note that all the session variables are case sensitive.   <o:p>

<o:p> 

<o:p> 

<o:p> 

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,<o:p>

<o:p> 

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click<o:p>

' Function To Display PopUp Window<o:p>

    Private Sub subDisplayPopUp()<o:p>

        Dim strJScript As String<o:p>

        strJScript = "<script language=javascript>"<o:p>

        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');"<o:p>

        strJScript += "</script>"<o:p>

        Response.Write(strJScript)<o:p>

<o:p> 

  ' Call Function To Display PopUp Window<o:p>

        subDisplayPopUp()<o:p>

    End Sub <o:p>

End Sub<o:p>

<o:p> 

This is the code for the subroutine used in the above code.<o:p>

<o:p> 

' Function To Display PopUp Window<o:p>

    Private Sub subDisplayPopUp()<o:p>

        Dim strJScript As String<o:p>

        strJScript = "<script language=javascript>"<o:p>

        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');"<o:p>

        strJScript += "</script>"<o:p>

        Response.Write(strJScript)<o:p>

    End Sub<o:p>

<o:p> 

<o:p> 

Finally, in your client page, add the following script, to get the values returned by the message box.<o:p>

<o:p> 

<script language="javascript"><o:p>

            function first(MsgBoxRetval)<o:p>

            {<o:p>

              document.Form1.txtReturnResult.value = MsgBoxRetval;<o:p>

            }<o:p>

</script><o:p>

<o:p> 

Here ‘txtReturnResult ’ is the name of textbox where I can the returned Value.<o:p>

The client Web Page Looks something like This,

<o:p> 

<o:p> 

<o:p>

<o:p>Sample screenshot

<o:p> 

<o:p> 

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.<o:p>

<o:p> 

<o:p> 

<o:p> 

Internal Working Of The control<o:p>

At page load of popup window you can find the following code,<o:p>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<o:p>

        'Put user code to initialize the page here<o:p>

<o:p> 

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

<o:p> 

        Me.imgMsgBox.ImageUrl = Session("imgUrl")<o:p>

<o:p> 

        Me.lblMsgBox.Text = Session("lblMsgBox")<o:p>

<o:p> 

        ' Call function to Display Push Buttons as Needed<o:p>

        funMsgBoxButtons(Session("intCapCode"))<o:p>

    End Sub<o:p>

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.<o:p>

The code for the function funMsgBoxButtons() is shown below,<o:p>

' Function which displays Ok,Yes,No,Cancel Captions on the Buttons<o:p>

    Public Function funMsgBoxButtons(ByVal intCapCode As Integer)<o:p>

<o:p> 

        ' Call Function to Display All Buttons, then Hide/Unhide them Accordingly<o:p>

        funVisibleButtons()<o:p>

<o:p> 

        Select Case intCapCode<o:p>

            Case 0<o:p>

                cmd1.Text = "Ok"<o:p>

                cmd2.Visible = False<o:p>

                cmd3.Visible = False<o:p>

            Case 1<o:p>

                cmd1.Text = "Ok"<o:p>

                cmd2.Text = "Cancel"<o:p>

                cmd3.Visible = False<o:p>

            Case 2<o:p>

                cmd1.Text = "Yes"<o:p>

                cmd2.Text = "No"<o:p>

                cmd3.Text = "Cancel"<o:p>

            Case 3<o:p>

                cmd1.Text = "Yes"<o:p>

                cmd2.Text = "No"<o:p>

                cmd3.Visible = False<o:p>

        End Select<o:p>

    End Function<o:p>

What Happens when U click on the Ok – Cancel – Yes – No Buttons :<o:p>

When U click on the Message Box buttons, the respective code is executed, There are three buttons on the Message Box,<o:p>

' When the caption is Ok or Yes<o:p>

    Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click<o:p>

        If cmd1.Text = "Ok" Then<o:p>

            <o:p>

            strReturnVal = 1<o:p>

        ElseIf cmd1.Text = "Yes" Then<o:p>

            <o:p>

            strReturnVal = 2<o:p>

        End If<o:p>

<o:p> 

        ' This Closes the Message Box<o:p>

        funCloseMsgBox()<o:p>

    End Sub<o:p>

<o:p> 

    ' When the caption is Cancel or No<o:p>

    Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click<o:p>

        If cmd2.Text = "Cancel" Then<o:p>

            <o:p>

            strReturnVal = 4<o:p>

        ElseIf cmd2.Text = "No" Then<o:p>

            strReturnVal = 3<o:p>

        End If<o:p>

<o:p> 

        ' This Closes the Message Box<o:p>

        funCloseMsgBox()<o:p>

    End Sub<o:p>

<o:p> 

    ' When the caption is Cancel<o:p>

    Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click<o:p>

        If cmd3.Text = "Cancel" Then<o:p>

            <o:p>

            strReturnVal = 4<o:p>

        End If<o:p>

<o:p> 

        ' This Closes the Message Box<o:p>

        funCloseMsgBox()<o:p>

    End Sub<o:p>

<o:p> 

    ' This Closes the Message Box and Returns the ... ... value <o:p>

    Private Sub funCloseMsgBox()<o:p>

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

    End Sub<o:p>

<o:p> 

Disadvantages Of this Control :<o:p>

<o:p> 

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

<o:p> 

Future Enhancments:<o:p>

<o:p> 

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.<o:p>

<o:p> 

<o:p> 

<o:p> 

<o:p> 

By Bye.<o:p>

<o:p> 

Shaik Anwar Hussain M. Tech (IT)<o:p>

Software Engineer,<o:p>

Holool India Ltd.,<o:p>

<o:p> 

E-mail : s_anwar786@rediffmail.com

<o:p> 

<o:p> 

<o:p> 

<o:p> 

<o:p> 

License

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


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

 
GeneralMy vote of 1 Pin
Syed J Hashmi4-Jul-11 17:19
Syed J Hashmi4-Jul-11 17:19 
QuestionInput string was not in a correct format. Pin
vyasv28-Aug-08 22:18
vyasv28-Aug-08 22:18 
Questionplease help Pin
Member 134440811-Mar-08 22:38
Member 134440811-Mar-08 22:38 
QuestionPopUp Window With Different Buttons Pin
Member 134440811-Mar-08 9:50
Member 134440811-Mar-08 9:50 
Generalsubect [modified] Pin
robocato24-Apr-07 23:12
robocato24-Apr-07 23:12 
GeneralVery Nice Article Pin
sukanya lamu20-Dec-06 1:05
sukanya lamu20-Dec-06 1:05 
GeneralReally a very good article, keep it up..!! Pin
Anonymous16-Oct-05 17:50
Anonymous16-Oct-05 17:50 
GeneralGood Pin
Anonymous27-Sep-05 0:27
Anonymous27-Sep-05 0:27 

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.