次の方法で共有


Creating ASP event handlers

Here's another tip from Rich Klein about how to
create event handler for classic ASP code.

If you’ve ever tried to
program an event handler for option buttons (also know as radio buttons) in
ASP, you know this can be a daunting task. Option buttons do not automatically
fire events in a way that can be easily trapped using VBScript. You must
specify the event handler for each event when defining the control.

For example, if you want
to populate a variable value when a particular option button is clicked, you
might try something like this:

 <input type="radio" name="rdoButton" OnClick="rdoButton_OnClick('12')">

Because this option button
is using a well-defined event tag, including a language attribute is not always
necessary, but in other cases, it may be necessary. For example:

 input type="radio" name="rdoButton" OnClick="rdoButton_OnClick('12')" language="vbscript">

Using a language
attribute ensures the ASP interpreter knows how to handle the event. Without the
attribute, the interpreter may try to parse the statement using rules for
JavaScript and produce an error when the statement does not conform.

Additionally, option buttons
have other quirks. The biggest one I’ve found so far is the fact that if you
are creating a dynamic list from a table of data, you would logically place the
button in a statement loop. If you define each button with a unique name—for
example, the name is terminated by the value of a loop counter—each option button
would operate on its own, allowing a user to click more than one button.

If you create each button
using the same variable name, then the buttons behave as intended, but now you
must access them as an array control, which means that you must use a
subscripted reference to the variable name. For example, using the preceding code,
"rdoButton(0)" would reference the first option button in the group.

The problem here is that
when you are dealing with dynamic data, you won’t always know how many records
are returned. This might not be an issue, but in the case in which only one
record is returned and there is only one option button, the control cannot be
referenced as a control array; it must be referenced as an individual control.
Referencing it as an array returns an error because there is no subscript to
reference.

To work around this
behavior, define the control with a value attribute and a parameter that
is passed in the event handler you define.

 <input type="radio" name="rdoButton" value="12" OnClick="rdoButton_OnClick('12')" language="vbscript">

Then when creating the
event handler in the script, do something like the following:

 <script language="vbscript">
   Sub rdoButton_OnClick(varNum)
      If IsNumeric(varNum) then
         form.hCurrentRecord.value = varNum
      Else
         form.hcurrentRecord.value = form.rdoButton.value
      End If
   End Sub
</script>

Note   The 'form'
designation is the name given to the form object in which the option buttons
are contained.

By using this format, you
avoid entirely the necessity of finding the array subscript of the option button
and the error produced if trying to reference the non-existent array value of a
single button. As a side note, if you place an alert in the first line of the
sub that displays the value of the variable passed in 'varNum', you will see
that, in the case of a single option button, the event fires twice. The first
time it displays the proper value passed, and the second time it displays a
reference to the object. This is why getting the value from the control is
necessary. This technique could probably be simplified further by just ignoring
the second event. Then the Else clause would not be required and the value
attribute in the option buttons could be eliminated.

Comments

  • Anonymous
    April 27, 2005
    I just found this post today, sorry to be late.

    Did you know that the event firing twice is a side-effect of the name of the onclick handler?
    I've just spent a couple hours trying to find out why a button on my web page fires twice. My button automates Excel, and doing everything twice is quite expensive, (and confusing to end users who get two copies of their spreadsheet).
    Here's what I found:
    If the name of the event handler ends with "_onclick", the event gets called twice.

    Simply changing the name of the event handler corrects this. I used "button_click"

    It is also seems to be tied to control array handling, because it will only fire twice on the last radio button in an array. (The only button in the special case of one button.)

    Try it out.
  • Anonymous
    February 28, 2006
    dfghdfhdfgh
  • Anonymous
    April 03, 2006
    I don't quite get what onclick event handlers would have to do with ASP code which is server side?