10
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. onclick() 2. onchange() 3. onkeydown() 4. onblur() 5. focus 6. checked 7. disabled 8. 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>

Event Handling in VBScript

  • Upload
    ajay-aj

  • View
    21

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Event Handling in VBScript

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. onclick()2. onchange()3. onkeydown()4. onblur()5. focus6. checked7. disabled8. 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.

Page 2: Event Handling in VBScript

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.focusend sub</script></BODY></HTML>

Page 3: Event Handling in VBScript

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.focusMyForm.btn.disabled=trueend sub</script></BODY></HTML>

If we want some form to be disabled until some other event doesn’t 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

Page 4: Event Handling in VBScript

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.focusMyForm.btn.disabled=trueend sub

sub text1_onkeydown()if len(MyForm.text1.value)>1900 thenmsgbox “you can not enter more then 20 characters”end ifend sub

</script></BODY></HTML>

So you can see that how easy it is for checking something on every key press.

Page 5: Event Handling in VBScript

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.focusMyForm.btn.disabled=trueend sub

sub text1_onkeydown()if len(MyForm.text1.value)>1900 thenmsgbox “you can not enter more then 20 characters”end ifend sub

sub text1_onblur()if MyForm.text1.value=”” thenmsgbox “this field is mendetory”exit subelse MyForm.btn.disabled=falseEnd ifend sub

</script></BODY></HTML>

So you can see that validation is so easy if you are using the events properly.

Page 6: Event Handling in VBScript

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.focusMyForm.btn.disabled=trueend sub

sub text1_onkeydown()if len(MyForm.text1.value)>1900 thenmsgbox “you can not enter more then 20 characters”end ifend sub

sub text1_onblur()if MyForm.text1.value=”” thenmsgbox “this field is mendetory”exit subelse MyForm.btn.disabled=falseEnd ifend sub

Page 7: Event Handling in VBScript

sub sl1_onchange()

if MyForm.sl1.value=”Rahul” thenmsgbox “You have selected Rahul”end if

if MyForm.sl1.value=”Arun” thenmsgbox “You have selected Arun”end if

if MyForm.sl1.value=”” thenmsgbox “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>

Page 8: Event Handling in VBScript

<script language=”vbscript”>sub window_onload()msgbox “I am loaded”MyForm.text1.focusMyForm.btn.disabled=trueend sub

sub text1_onkeydown()if len(MyForm.text1.value)>1900 thenmsgbox “you can not enter more then 20 characters”end ifend sub

sub text1_onblur()if MyForm.text1.value=”” thenmsgbox “this field is mendetory”exit subelse MyForm.btn.disabled=falseEnd ifend sub

sub sl1_onchange()

if MyForm.sl1.value=”Rahul” thenmsgbox “You have selected Rahul”end if

if MyForm.sl1.value=”Arun” thenmsgbox “You have selected Arun”end if

if MyForm.sl1.value=”” thenmsgbox “You have selected Blank”end ifend sub

sub btn_onclick()msgbox “You have submitted the form”MyForm.SubmitEnd sub

</script></BODY></HTML>

Page 9: Event Handling in VBScript

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 [email protected] please mail me your views.

Rahul G. Dubey.Date:- 07/17/2003