Sie sind auf Seite 1von 9

Event Handling in VBScript.

We usually use vbscript in ASP as Server Side Scripting and JavaScript as Client Side Scripting, but I think if we use one scripting through out the code, it is much more easier to understand and maintain. This can not be done in JavaScript, so I thought that lets try it in VBScript and here I am sharing my experience with all of you. Event Handling through VBScript is very easy in VBScript. Lets see what are the most common events we handle while developing a web page. 1. 2. 3. 4. 5. 6. 7. 8. onclick() onchange() onkeydown() onblur() focus checked disabled Window_onload()

HTML Part You need to give an unique ID to each form, you are using in your page. Ex. <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <input type=button id=btn name=btn value=Submit> </Form> </BODY> </HTML> Here in above example you can see that I have given an ID to each form.

Lets see that if you want to show a massagebox when the page gets load for the first time. Window_onload() Event Ex. <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded end sub </script> </BODY> </HTML> Now whenever this page will get load or refreshed the massage box will be displayed. You can write whatever you want to be done when the page gets loaded under this subroutine. Focus Property of the form Ex. <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded MyForm.text1.focus end sub </script> </BODY>

</HTML> Sometimes we want to set focus on a particular form whenever the page gets loaded, this can be done from this simple line under subroutine window_onload() MyForm.text1.focus Now whenever the page gets loaded the focus will be set to text1. Disabled Property of the form Ex. <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded MyForm.text1.focus MyForm.btn.disabled=true end sub </script> </BODY> </HTML> If we want some form to be disabled until some other event doesnt occur and enable it, we can do it by this simple line MyForm.btn.disabled=true And whenever we want to enable it we just have to write MyForm.btn.disabled=false

Checked Property of the form Like disable also we can also check or uncheck any check box MyForm.check1.checked=true MyForm.check1.checked=false

onkeydown() Event Onkeydown() event is used where we want to check something whenever a key is being hit. Ex. Like if we want to check the value of text1 not to exceed 20, then this is the example <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded MyForm.text1.focus MyForm.btn.disabled=true end sub sub text1_onkeydown() if len(MyForm.text1.value)>1900 then msgbox you can not enter more then 20 characters end if end sub </script>

</BODY> </HTML> So you can see that how easy it is for checking something on every key press.

onblur() Event onblur() event is used when we want something to be done when a form lost the focus. Like if we want that if the submit button gets enabled when text1 lost its focus then we will use onblur(). Ex. <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded MyForm.text1.focus MyForm.btn.disabled=true end sub sub text1_onkeydown() if len(MyForm.text1.value)>1900 then msgbox you can not enter more then 20 characters end if end sub sub text1_onblur() if MyForm.text1.value= then msgbox this field is mendetory exit sub else

MyForm.btn.disabled=false End if end sub </script> </BODY> </HTML> So you can see that validation is so easy if you are using the events properly. Onchange() Event I love this event when I have to use listbox, because I can fire any event based on the changed value of listbox. Ex. <HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <Select name=sl1 id=sl1 > <option value=></option> <option value=Rahul>Rahul</option> <option value=Arun>Arun</option> </select> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded MyForm.text1.focus MyForm.btn.disabled=true end sub sub text1_onkeydown() if len(MyForm.text1.value)>1900 then

msgbox you can not enter more then 20 characters end if end sub sub text1_onblur() if MyForm.text1.value= then msgbox this field is mendetory exit sub else MyForm.btn.disabled=false End if end sub sub sl1_onchange() if MyForm.sl1.value=Rahul then msgbox You have selected Rahul end if if MyForm.sl1.value=Arun then msgbox You have selected Arun end if if MyForm.sl1.value= then msgbox You have selected Blank end if end sub </script> </BODY> </HTML> So you can see the power of onchange event, then go and use it guys. Onclick() Event Onclick() Event is used for action taken after clicking on some form, you can use it with buttons, check boxes, radio buttons etc I am giving you the example for on button click

<HTML> <BODY> <Form id=MyForm name=MyForm action=test.htm> <input type=text id=text1 name=text1> <Select name=sl1 id=sl1 > <option value=></option> <option value=Rahul>Rahul</option> <option value=Arun>Arun</option> </select> <input type=button id=btn name=btn value=Submit> </Form> <script language=vbscript> sub window_onload() msgbox I am loaded MyForm.text1.focus MyForm.btn.disabled=true end sub sub text1_onkeydown() if len(MyForm.text1.value)>1900 then msgbox you can not enter more then 20 characters end if end sub sub text1_onblur() if MyForm.text1.value= then msgbox this field is mendetory exit sub else MyForm.btn.disabled=false End if end sub sub sl1_onchange() if MyForm.sl1.value=Rahul then msgbox You have selected Rahul end if

if MyForm.sl1.value=Arun then msgbox You have selected Arun end if if MyForm.sl1.value= then msgbox You have selected Blank end if end sub sub btn_onclick() msgbox You have submitted the form MyForm.Submit End sub </script> </BODY> </HTML> So you can see that validation is so easy with event handling, and why I love using VBScript because I have to use it for Server Site Scripting so why unnecessary use JavaScript, why not do it in the VBScript only. Hope you people will like the article. This site helped me a lot that is why I sharing this thing with all of you. Since this is my first article I will expect lots of feedback from you guys, my email address is rgdubey@rediffmail.com please mail me your views. Rahul G. Dubey. Date:- 07/17/2003

Das könnte Ihnen auch gefallen