[NEWBIE] Call an event from a javascript

T

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()"
 
C

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.
 
T

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>
 
J

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.
 
T

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()"



*****************************************
 
L

Laurent Bugnion

Hi,
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
 
C

Clinton Farleigh

Laurent,

you are mostly right. If you look at the Render method of
AttributesCollection (through .NET Reflector or ILDASM), you will
notice that attributes which are unknown to ASP.NET are written out to
the rendered html by default. What teo is doing with the onkeydown
event handler will technically work.

A form element could have mySubToCall if teo added it to that scope. I
didn't look at his code in detail so I don't know if he did.

Teo I suggest you spend some time understanding Javascript and how
ASP.NET works with it.

clint.
 
T

teo

A form element could have mySubToCall if teo added it to that scope. I
didn't look at his code in detail so I don't know if he did.


The fact is I'm not going to use javascript very often,
so I'm not going to understand this language.

My current needs is very simple:

let the user press the Canc key
on a Listbox item
and fire the code
in the
Button1_Click event
and in the
Public Sub Night() routine.


I thought it wasn't an hard task,
so I asked help.
I need the last step:
from the function zzzzz to the
Button1_Click and Public Sub Night()


This is the simple code in the
'Default.aspx.vb'
file :




Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox("goodMorning")
End Sub

Public Sub Night()
MsgBox("goodNight")
End Sub

End Class




and this code is the simple code
in the
'Default.aspx'
source :




<%@ 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>Pagina senza titolo</title>
</head>

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

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

if ( evt.keyCode != null
&& evt.keyCode == 46 )
{
document.form.Night()
document.form.Button1_Click()
}
}


// -->
</script>


<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" onkeydown"zzzzz"
Style="z-index: 100; left: 40px; position: absolute;
top: 40px">
<asp:ListItem>aaaa</asp:ListItem>
<asp:ListItem>bbbb</asp:ListItem>
<asp:ListItem>cccc</asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Style="z-index: 102; left:
168px; position: absolute;
top: 68px" Text="Button" />

</div>
</form>
</body>
</html>
 
C

Clinton Farleigh

i think you are going to have to understand how asp.net interacts with
client side javascript. As far as I can tell, what you are trying to
do is to call a server method from the client side. This doesn't make
sense.

I think what you really want is to submit to the server side when your
user selects a different item in the listbox control. What you really
want to do is throw out that javascript and change the autopostback
attribute of your listbox to true. This way, asp.net will generate
some javascript for you that will automatically post back the page to
the server and you don't need to understand how it works.

However, I don't even know if this is what you really want. The MsgBox
thing leads me to believe you want javascript alerts to show up when
zzzzz function gets executed.

I'd suggest that you at least start with some html and javascript
without putting asp.net into the mix. you are only confusing yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top