Home All Groups Group Topic Archive Search About

[NEWBIE] Call an event from a javascript

Author
8 Sep 2006 2:00 AM
teo
Hallo

Newbie question about syntax.

I'd like to fire the zzzzz event
when the user presses the 'Canc' key
on a Listbox item.

--------------------------------

I added:
onkeydown="javascript:zzzzz"
to the
<asp:ListBox
tag

but I got a green underscore
How can I avoid that green underscore?

If I ignore it, no problem seems to occur.
Does it really cause no problem?


--------


Also I added this:


<script language="javascript" type="text/javascript">
<!--

function zzzzz()
{
var i;
i= window.event.keyCode
if(i=46)
{
    alert("Canc key is pressed");
        this.form.mySubToCall()
        this.form.Button1_Click()

}
}


// -->
</script>




the first event is regularly fired (alert),
but the second and the third ones are not fired,
and I got two error messages
(note: the Button1 is simply a control I placed on the form
and the  mySubToCall  is a simple Public Sub I manually wrote)


What have I to do in order to fire two events?


--------------------------


Also I'm wondering if
instead of
onkeydown="javascript:zzzzz"
I directly could call   mySubToCall()  and  Button1_Click()

a sort of:
onkeydown="javascript:mySubToCall()"
and
onkeydown="javascript:Button1_Click()"


--------------------------


Thanks

Author
8 Sep 2006 2:36 AM
Clinton Farleigh
You don't necessarily need the "javascript:" prefix on events.  I'm not
sure about the green underscore, but it may be due an improperly formed
tag.  Does it go away if you do remove the onkeydown attribute?

this.form.Button1_Click() probably should be
document.form.Button1.Click() and also make sure that mySubToCall is
actually declared in the context of the form.  something like:
document.form.mySubToCall = mySubToCall;

Also note that ASP.NET renders controls as html elements that may have
a different id than their server side id.   Do a view source in the
browser to determine what the rendered id is of your control.  You can
probably ignore this until you start putting controls inside user
controls or other naming containers.

teo wrote:
Show quoteHide quote
> Hallo
> .
> Newbie question about syntax.
>
> I'd like to fire the zzzzz event
> when the user presses the 'Canc' key
> on a Listbox item.
>
> --------------------------------
>
> I added:
> onkeydown="javascript:zzzzz"
> to the
> <asp:ListBox
> tag
>
> but I got a green underscore
> How can I avoid that green underscore?
>
> If I ignore it, no problem seems to occur.
> Does it really cause no problem?
>
>
> --------
>
>
> Also I added this:
>
>
> <script language="javascript" type="text/javascript">
> <!--
>
> function zzzzz()
> {
> var i;
> i= window.event.keyCode
> if(i=46)
> {
>     alert("Canc key is pressed");
>         this.form.mySubToCall()
>         this.form.Button1_Click()
>
> }
> }
>
>
> // -->
> </script>
>
>
>
>
> the first event is regularly fired (alert),
> but the second and the third ones are not fired,
> and I got two error messages
> (note: the Button1 is simply a control I placed on the form
> and the  mySubToCall  is a simple Public Sub I manually wrote)
>
>
> What have I to do in order to fire two events?
>
>
> --------------------------
>
>
> Also I'm wondering if
> instead of
> onkeydown="javascript:zzzzz"
> I directly could call   mySubToCall()  and  Button1_Click()
>
> a sort of:
> onkeydown="javascript:mySubToCall()"
> and
> onkeydown="javascript:Button1_Click()"
>
>
> --------------------------
>
>
> Thanks
Are all your drivers up to date? click for free checkup

Author
8 Sep 2006 3:21 PM
teo
I keep on failing.


I also tried
to rename
Protected Button1_Click   in    Public Button1_Click
and
to pass some parameters, such
document.form.Button1_Click(1,1)


If you want to try it, here the simple code:



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>No title</title>

    <script language="vbscript" type="text/vbscript">
sub zzz()
dim i
i= window.event.keyCode
if i=13 then  ' = the Enter key

    'THE MESSAGEBOX IS REGULARLY FIRED  --  (I'm not interested in it):
    ' msgbox "Enter Key is Pressed"


    'THE BUTTON EVENT IS NOT CALLED  --  (this is what I'm interested in
the most):
    ' call me.Button1_Click(1,1)
    ' me.Button1_Click()
    ' document.form.Button1.Click()
    document.form.Button1_Click()


    'THE SUB EVENT IS NOT CALLED  --  (I presume this requires me to write
here
    'the related  mySubHallo  in JavaScript and I'm not able to do that):
    ' mySubHallo()
    document.form.mySubHallo()

end if
end sub

</script>



</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" onkeydown="zzz"
Style="z-index: 100; left: 48px; position: absolute;
            top: 56px">
            <asp:ListItem>11111</asp:ListItem>
            <asp:ListItem>22222</asp:ListItem>
            <asp:ListItem>33333</asp:ListItem>
        </asp:ListBox>
        <asp:Button ID="Button1" runat="server" Style="z-index: 101; left:
296px; position: absolute;
            top: 76px" Text="Button" />
        &nbsp;

    </div>
    </form>
</body>
</html>
Author
10 Sep 2006 1:37 PM
teo
None here for this problem?
Author
10 Sep 2006 1:44 PM
Juan T. Llibre
You're using vbscript, not javascript:

> <script language="vbscript" type="text/vbscript">

Please post a question that makes sense in the context of your subject.




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Show quoteHide quote
"teo" <t**@inwind.it> wrote in message news:q958g2tla6o0pi9kafdjh8r4sori7ibn7l@4ax.com...
> None here for this problem?
Author
10 Sep 2006 9:10 PM
teo
Oh sorry,
I cut all just to not waste space:
Here my very first post:



*****************************************

Hallo,
I'd like to fire the zzzzz event
when the user presses the 'Canc' key
on a Listbox item.

---------------------------

I added:
onkeydown="zzzzz"
to the
<asp:ListBox
tag

but I got a green underscore
How can I avoid that green underscore?

If I ignore it, no problem seems to occur.
Does it really cause no problem?


------


Also I added this:


<script language="javascript" type="text/javascript">
<!--

function zzzzz()
{
var i;
i= window.event.keyCode
if(i=46)
{
    alert("Canc key is pressed");
        document.form.mySubToCall()
        document.form.Button1_Click()

}
}


// -->
</script>




the first event is regularly fired (alert),
but the second and the third ones are not fired,
and I got two error messages
(note: the Button1 is simply a control I placed on the form
and the  mySubToCall  is a simple Public Sub I manually wrote)


What have I to do in order to fire two events?


Note:

I also tried
to rename
   Protected Button1_Click   in    Public Button1_Click
and
to pass some parameters, such
  document.form.Button1_Click(1,1)
but obtained nothing.


--------------------------


Also I'm wondering if
instead of
onkeydown="zzzzz"
I directly could call   mySubToCall()  and  Button1_Click()

a sort of:
onkeydown="mySubToCall()"
and
onkeydown="Button1_Click()"



*****************************************
Author
11 Sep 2006 5:37 AM
Laurent Bugnion
Hi,

teo wrote:
Show quoteHide quote
> Oh sorry,
> I cut all just to not waste space:
> Here my very first post:
>
>
>
> *****************************************
>
> Hallo,
> I'd like to fire the zzzzz event
> when the user presses the 'Canc' key
> on a Listbox item.
>
> ---------------------------
>
> I added:
> onkeydown="zzzzz"
> to the
> <asp:ListBox
> tag
>
> but I got a green underscore
> How can I avoid that green underscore?
>
> If I ignore it, no problem seems to occur.
> Does it really cause no problem?

The ListBox control does not have a "onkeydown" event handler, and even
if it had, it would be a server-side event handler only.

To add a client-side attribute to an ASP.NET control, you use the
Control.Attributes collection, and its "Add" method.

> Also I added this:
>
>
> <script language="javascript" type="text/javascript">
> <!--
>
> function zzzzz()
> {
> var i;
> i= window.event.keyCode
> if(i=46)

JavaScript has a C-like syntax, and the comparison operator is "==", not
"=".

zzzzz() is probably the worst method name. A method (or function, in
this case) name should be a verb describing what the method does.

Note also that this code is IE-specific. For a browser-compatible code
between Firefox and IE:

function zzzzzz( evt )
{
   if ( window.event )
   {
     evt = window.event;
   }

   if ( evt.keyCode != null
     && evt.keyCode == 46 )
   {
     // ...
   }
}

> {
>     alert("Canc key is pressed");
>         document.form.mySubToCall()

A Form element doesn't have a mySubToCall method.

At this point, I got to stop. Your code just doesn't make sense. I
recommend you to take some time and study JavaScript and its
interactions with ASP.NET. It's important that you understand what
you're doing. In your situation, asking the newsgroup is not enough to
help you.

I recommend you David Flanagan's "JavaScript, the definitive guide",
which was just released in its 5th edition.
http://www.oreilly.com/catalog/jscript5/

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Bookmark and Share