PopUp Window With Different Buttons ( Windows Message Box )






2.34/5 (15 votes)
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,
( Message Box with OK Button )
( Message Box with Yes No Buttons )
( Message Box with Ok Cancel Buttons )
( 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,
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