Introduction
Using dialog box in any application is very important in order to have close talk between the user and the application. These dialogs between the user and the application greatly enhance the usability of the application.
Dialog box can be used to give some feedback to the user or to get some input from the user or even both.
It was really a kids work to implement a dialog box in the window based application. Yes! It was very simple
Dim fchild as new frmchild Frmchild. Show vbmodal |
|
How easy it was ??? But, how about opening the same dialog box in the web based application? Oops! Its require some bit technique to open it, specially if you want to open a modal dialog box.
In the following article, we will see some of the important technique of implementing dialog box in the web based application.
Simple dialog box
The first and usual technique of implementing dialog box is to put JavaScript to the participating event (usually click event) of the control. its really a easy way when your application already know when to open a dialog box when the page load to the browser.
Here is the simple example, put a button form control to the web page and insert the following code.
Btnopen1.attributes. Add ("onclick", "alert ('Got it? ');") |
|
Its a one line code to pop a alert message to the user.
Same way, suppose you want that, the application open a confirmation dialog box from where the user can select his option. you can use this method very well. like, you want to open a confirmation dialog box when user click on the delete button of the page. simply use following code for that:
Btndelete. Attributes. Add ("onclick", "If (confirm ('Are you sure to delete? ') {} Else {return false }") |
|
It will create a small JavaScript code to the browser. when user select "no" from the confirmation dialog box, it will return false. hence nothing will happen. and if user select "yes", it will return true and will post the page.
After little bit of simple alert and confirmation dialog box, we will entered into some serious work of opening dialog box.
Open dialog box with another web form
Suppose, you want to open an another web page as a dialog box, then what will you do? Here is the same coding with little difference.
Btnopen2.attributes. Add ("onclick", "window. Open ('child. aspx ')") |
|
Btnopen is again a web form control and when user click on this button, it will open another instance of the browser with the page "child. aspx ".
Its really working good, until you will notice that it create one another instance of the browser, put one more application to the task bar, even the dialog box is modeless. is it not possible to open a page in the same browser with modal dialog box? Sure! Here is the code for that
Btnopen3.attributes. add ("onclick", "window. showmodaldialog ('child. aspx ', null, 'status: No; dialogwidth: 370px; dialogheight: 220px; dialoghide: true; help: No; scroll: no ');") |
|
What does above Code actually do? Nothing! It simply create a small JavaScript code to the page and attach the click even of the control to that script. if you look at the HTML source code of the page, then you will find we You Want To Know/here is the HTML code of page that is create with above dialog box Popup.
Returning Value From the dialog box
OK! What about returning any value from the dialog box? Suppose you have opened a dialog box for some purpose and you want that it returns some value to the parent web form.
For returning any thing to the parent window from the Child dialog box, JavaScript provides one attribute of the window object, that is
Window. returnvalue
To understand its working. Let create a small web project with two web forms named:
Parent. aspx
Child. aspx
Parent web form will have one textbox and a button. Look at the following code of the parent web form.
Parent. aspx
<Body ms_positioning = "gridlayout"> <Form ID = "form1" method = "Post" runat = "server"> <Asp: textbox id = "txtvalue" style = "Z-INDEX: 101; left: 16px; position: absolute; top: 24px" runat = "server"> </ASP: textbox> <Asp: button id = "btnopen" style = "Z-INDEX: 102; left: 176px; position: absolute; top: 24px" runat = "server" text = "open... "> </ASP: button> </Form> </Body> |
|
Parent. aspx. VB
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load Btnopen. attributes. add ("onclick", "Var strreturn; strreturn = Window. showmodaldialog ('child2. aspx ', null, 'status: No; dialogwidth: 370px; dialogheight: 220px; dialoghide: true; help: No; scroll: no'); If (strreturn! = NULL) document. getelementbyid ('txtvalue'). value = strreturn ;") End sub |
|
And, child web form which will have a textbox and two buttons. Look at the following code of the Child page.
Child2.aspx
<Body ms_positioning = "gridlayout"> <Form ID = "form1" method = "Post" runat = "server"> <Asp: textbox id = "txtvalue" style = "Z-INDEX: 101; left: 16px; position: absolute; top: 24px" runat = "server"> </ASP: textbox> <Asp: button id = "btnok" style = "Z-INDEX: 103; left: 48px; position: absolute; top: 56px "runat =" server "text =" OK "width =" 56px "> </ASP: button> <Asp: button id = "btncancel" style = "Z-INDEX: 102; left: pixel; position: absolute; top: 56px "runat =" server "text =" cancel "> </ASP: button> </Form> </Body> |
|
Child2.aspx. VB
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load Btnok. Attributes. Add ("onclick", "window. returnvalue = Document. getelementbyid ('txtvalue'). value; window. Close ();")Btncancel. Attributes. Add ("onclick", "window. Close ();") End sub |
|
The purpose of this small module is, that when user click on the button of the parent form, it will open child. apsx file in the dialog box. in the child form user will enter some value in the textbox and it he click on the OK button it will return that value to the parent form. with that it will update the textbox of the parent form. and of course, if user click on the cancel button, it will simply close the child dialog box without doing anything.
In all of the above case, there is nothing to do with the ASP. NET. All of things are actually handle by the Javascript. See the JavaScript code of the Child web form.
<Input name = "txtvalue" type = "text" id = "txtvalue" style = "Z-INDEX: 101; left: 16px; position: absolute; top: 24px"/> <Input type = "Submit" name = "btnok" value = "OK" id = "btnok" onclick = "window. returnvalue = document. getelementbyid ('txtvalue '). value; window. close (); "style =" width: 56px; Z-INDEX: 103; left: 48px; position: absolute; top: 56px "/> <Input type = "Submit" name = "btncancel" value = "cancel" id = "btncancel" onclick = "window. close (); "style =" Z-INDEX: 102; left: pixel PX; position: absolute; top: 56px "/> |
|
In the child web form, the value to be return is assigned to the window. returnvalue. and the return value is assigned to a variable in the parent web form. thereafter, it assigned to the textbox.
The idea is that, window. returnvalue returns some value from the Child Window to the parent window, and thereafter, you can do any thing with that returned value. you can even pass to the VB code using parameter of the query string of the document action.
There will be one limitation of using abve method of returning value from the dialog box. And the limitation is that you can only return one value to the parent window.
What about if I want to return more then one value?
I am sorry, there is no remedy of that, but I usually overcome with this limitation by using session object. I simply put all values to the session object in the Child Window and there after I handle it in the parent window.
One thing you must have notice that I have used response object for closing the dialog box. writing Javascript script to the response object is another way to put JavaScript to the web form. in above code it simply create script, which contains window. close () Statement, and write it to the browser. at client side of the browser, browser read the script and execute it.
So, from the last example we learned new way to put JavaScript to the browser and that is using response object.
Yes, you can use response object to popup A alert message also. Here is the code for that:
Private sub btnopen5_click (byval sender as system. Object, byval e as system. eventargs) handles btnopen5.click Dim strscript as string = "" Strscript = "<SCRIPT>" Strscript = strscript & "alert ('simple alter message !!!! ');" Strscript = strscript & "</SCRIPT>" Response. Write (strscript) End sub |
|
Above code, simply pop an alert message to the user.
If you want to display some variable message in the alert message then it you can do that with slight change.
Private sub btnopen6_click (byval sender as system. Object, byval e as system. eventargs) handles btnopen6.click Dim strscript as string = "" Dim INTSUM as integer Strscript = "<SCRIPT>" INTSUM = 344 + 233 Strscript = strscript & "alert ('sum:" & INTSUM &"');" Strscript = strscript & "</SCRIPT>" Response. Write (strscript) End sub |
|
There will be a drawback again, in using abve method to pop Alert box, you will notice that if you have tried the above Code. We will talk about it later on.
Same thing can be done by registering JavaScript code to the browser. Following is the example, will do the same thing, but here we will register the JavaScript to the page.
Private sub btnopen7_click (byval sender as system. Object, byval e as system. eventargs) handles btnopen7.click Dim strscript as string = "" Strscript = "<SCRIPT>" Strscript = strscript & "alert ('simple alter message !!!! ');" Strscript = strscript & "</SCRIPT>" Page. registerclientscriptblock ("clientscript", strscript) End sub |
|
Now about that drawback, yes, you are right, it wash screen of the parent window when it pop the alert message or dialog box. the all controls of the parent window will remain hidden unless you close the Alert box or dialog box.
Why ?? Its because, the Javascript that is generated is attached to the top of the HTML page. You can easily understand this you see the generated HTML code of the page. Look at the following:
This code is generated using response object:
<SCRIPT> alert ('simple alter message !!!! '); </SCRIPT> <HTML> <Head> <Title> webform1 </title> </Head> <Body ms_positioning = "gridlayout">
</Body> </Html> |
|
Or the code generated using registerclientscriptblock method:
<HTML> <Head> <Title> webform1 </title> </Head> <Body ms_positioning = "gridlayout"> <Form name = "form1" method = "Post" Action = "mainform. aspx" id = "form1"> <SCRIPT> alert ('simple alter message !!!! '); </SCRIPT>
</Form> </Body> </Html> |
|
In the last case the code is generated just below the form tag, but still its above the all HTML and Web controls. when the alert message or any modal dialog box is opened the focus will gone to the dialog box and rest of the controls will not visible as they are not rendered. when user close the alert message or ant modal dialog box, thereafter the rest of the control rendered.
SO, what is I want to display modal dialog box with the populated controls in the parent forms. its possible if you can put your generated JavaScript below the controls tags in the form. is it possible ?? Of course yes. here is the technique. use registerstartupscript method to put your JavaScript to the page. look at the following code of display an alert message and a modal dialog box. if you look at the generated HTML code then you will easily understand the working behind this technique. here is the generated HTML code.
<HTML> <Head> <Title> webform1 </title> </Head> <Body ms_positioning = "gridlayout"> <Form name = "form1" method = "Post" Action = "mainform. aspx" id = "form1">
<SCRIPT> alert ('simple alter message !!!! '); </SCRIPT> </Form> </Body> </Html> |
|
Generated Javascript is attached just above the closing tag of the form element and below of all HTML or Web controls tag. so, first all of the HTML controls will render and there after the modal dialog box or modal dialog box will open.
Conclusion
implementing dialog box in the web based application makes your application more usable. As it allow your application to have dialogs with the user more closely.