how to start sub from javascript?

B

Bob

Hi,

The user must enter a name into a text input control. Then i want to give
him the choice between clicking on the button with the mouse or pushing the
ENTER key of the keyboard.

This works when clicking on the button. My problem is that i don't know how
to start the sub 'pseudoserver' when pressing the ENTER key.

Here the code:

the aspx file:
------------
<input id="pseudo" type="text" onkeypress="pseudoclient(event)" />
<br />
<asp:Button ID="pseudoSubmit" runat="server" OnClick="pseudoserver" />

<script type="text/javascript">
function pseudoclient(touche)
{
var codetouche
touche=event
codetouche=touche.keyCode
if(codetouche == 13)
{
????
}
}
</script>

code-behind:
------------
Sub pseudoserver(ByVal sender As Object, ByVal e As System.EventArgs)
....
End sub

Thanks
Bob
 
R

Riki

The Enter key will activate the default button on the form.
Normally, this is the first button on the form.

You can set another button as the default by setting the form's
DefaultButton property:

<form runat="server" DefaultButton="pseudoSubmit" >

Riki
 
B

Bob

Thanks, but unfortunately, this page is a content page linked to a
masterpage. And there is no <form> in it.
 
P

PJ on Development

Hi, Bob

I think you can do it through client script.

---- BEGIN CODE ----
<script type='text/javascript'>
function TextBox_onKeyPress(e, obj)
{
e = e ? e : window.event;
if (e.keyCode != 13)
return;

var oForm = obj;
while (oForm && oForm.tagName != "FORM")
oForm = oForm.parentNode;

if (oForm)
oForm.submit();
}
</script>
....
<input type='text' onkeypress='TextBox_onKeyPress(event, this)' />
---- END CODE ----

Regards,

Paulo Santos
http://pjondevelopment.50webs.com
 
J

JinWeiJie

sorry, my code is in c#

in the code behind file:
txtControl.Attributes.Add("onkeydown", "pseudoclient");

in js:

function pseudoclient(touche)
{
var codetouche
touche=event
codetouche=touche.keyCode
if(codetouche == 13)
{
document.getElementById("<%=this.pseudoSubmit.ClientID
%>").click();
}
}


may it help :)
 
B

Bob

Thanks for the vb version..
I did it, but there must still be a problem: i have no error of any kind,
but when pushing on ENTER, nothing happens. Clicking on the button still
works.
Here my actual code:

aspx:
----
<asp:TextBox id="pseudo" runat="server"></asp:TextBox>
<asp:Button ID="pseudoSubmit" runat="server" />

<script type="text/javascript">
function pseudoclient(touche)
{
var codetouche
touche=event
codetouche=touche.keyCode
if(codetouche == 13)
{
document.getElementById("<%=pseudoSubmit.ClientID%>").click();
}
}
</script>

and in code-behind:
-------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
pseudo.Attributes.Add("onkeydown", "pseudoclient")
End Sub
 
D

Dan

Here it is: (maybe i didn't tell it's a content page linked to a
masterpage):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="ctl00_HeadArea"><link href="App_Themes/vert/vert.css"
type="text/css" rel="stylesheet" /><title>
Chatcenter van departement CO
</title><style type="text/css">
.ctl00_ctl00_Menu1_0 {
background-color:white;visibility:hidden;display:none;position:absolute;left:0px;top:0px;
}
.ctl00_ctl00_Menu1_1 { text-decoration:none; }
.ctl00_ctl00_Menu1_2 { }

</style></head>
<body>
<form name="aspnetForm" method="post" action="login3.aspx"
id="aspnetForm">
<div>
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJOTI1MjgxNjUyD2QWAmYPZBYCAgMPZBYEAgEPZBYCZg88KwANAgAPFgIeC18hRGF0YUJvdW5kZ2QMFCsAAgUDMDowFCsAAhYOHgRUZXh0BQlDaGF0cm9vbXMeBVZhbHVlBQlDaGF0cm9vbXMeC05hdmlnYXRlVXJsBRAvY2hhdC9zdGFydC5hc3B4HgdFbmFibGVkZx4KU2VsZWN0YWJsZWceCERhdGFQYXRoBRAvY2hhdC9zdGFydC5hc3B4HglEYXRhQm91bmRnFCsABAULMDowLDA6MSwwOjIUKwACFg4fAQUKTmV0aXF1ZXR0ZR8CBQpOZXRpcXVldHRlHwMFGi9jaGF0L2NoYXQvbmV0aXF1ZXR0ZS5hc3B4HwRnHwVnHwYFGi9jaGF0L2NoYXQvbmV0aXF1ZXR0ZS5hc3B4HwdnZBQrAAIWDh8BBQZVaXRsZWcfAgUGVWl0bGVnHwMFFi9jaGF0L2NoYXQvdWl0bGVnLmFzcHgfBGcfBWcfBgUWL2NoYXQvY2hhdC91aXRsZWcuYXNweB8HZ2QUKwACFg4fAQUWTGlqc3QgYWN0aWV2ZSBjaGF0dGVycx8CBRZMaWpzdCBhY3RpZXZlIGNoYXR0ZXJzHwMFFS9jaGF0L2NoYXQvbGlqc3QuYXNweB8EZx8FZx8GBRUvY2hhdC9jaGF0L2xpanN0LmFzcHgfB2dkZAIDD2QWAgICDw9kFgIeCm9uS2V5UHJlc3MFE3BzZXVkb2NsaWVudChldmVudClkZAAFlsX5toSBBVPhQKrCGHBrHkPy"
/>
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>


<script
src="/chat/WebResource.axd?d=fR-B6mdb3zMW33vwyvkDdw2&amp;t=633298597310000000"
type="text/javascript"></script>


<script
src="/chat/WebResource.axd?d=OqlxnU2t399cVgtpjT19Jg2&amp;t=633298597310000000"
type="text/javascript"></script>
<script
src="/chat/WebResource.axd?d=4nnjkKbvtMHy1JuIt0pIFA2&amp;t=633298597310000000"
type="text/javascript"></script>
<div id="masterp">
<div id="header1">

<div id="hfd" style="text-align: center">
<font size="6pt">Chatcenter van departement CO</font><br />
<font size="1pt"><br />
philippe mairiaux © 2007</font>
<br /><br />
</div>
<div style="height:5px; overflow:hidden"></div>
<!--
overflow:hidden est nécessaire pour IE 6 (pas pour IE 7)
-->
<table id="navig"><tr><td>
<a href="#ctl00_ctl00_Menu1_SkipLink"><img alt="Skip Navigation Links"
src="/chat/WebResource.axd?d=uRPfErkhbPHwfPIGCB4xLg2&amp;t=633298597310000000"
width="0" height="0" style="border-width:0px;" /></a><table
id="ctl00_ctl00_Menu1" class="ctl00_ctl00_Menu1_2" cellpadding="0"
cellspacing="0" border="0">
<tr>
<td onmouseover="Menu_HoverRoot(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n0"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/start.aspx">Chatrooms</a></td>
</tr>
</table></td><td style="width:3px;"></td><td><table border="0"
cellpadding="0" cellspacing="0" width="100%">
<tr>
<td onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n1"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/chat/netiquette.aspx"
style="margin-left:16px;">Netiquette</a></td>
</tr>
</table></td><td style="width:3px;"></td><td
onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n2"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/chat/uitleg.aspx" style="margin-left:16px;">Uitleg</a></td>
</tr>
</table></td><td style="width:3px;"></td><td
onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n3"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/chat/lijst.aspx" style="margin-left:16px;">Lijst actieve
chatters</a></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table><a id="ctl00_ctl00_Menu1_SkipLink"></a></td></tr></table>


</div>
<div id="maincontent">

<br /><br />
<center>
<fieldset style="width:300px; border-color:#008080">
<br />
Voer een pseudo om mee te chatten!
<h6>(Duw op Enter-toets of klik op de knop)</h6>
<input name="ctl00$main$pseudo" type="text" value="ddd"
id="ctl00_main_pseudo" onKeyPress="pseudoclient(event)" />
<br /><br />
<input type="submit" name="ctl00$main$pseudoSubmit" value="Voer in"
id="ctl00_main_pseudoSubmit" /><br />
<br />
<br />
</fieldset></center>

<script type="text/javascript">
function pseudoclient(touche)
{
var codetouche
touche=event
codetouche=touche.keyCode
if(codetouche == 13)
{
document.getElementById("ctl00_main_pseudoSubmit").click()
}
}

</script>

</div>
</div>

<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
value="/wEWAwKK2ubGCwLrsaj7BAKLte7vBGt9iu7wNVoc9I6/yVRp4lKBTXpU" />
</div>

<script type="text/javascript">
<!--
var ctl00_ctl00_Menu1_Data = new Object();
ctl00_ctl00_Menu1_Data.disappearAfter = 500;
ctl00_ctl00_Menu1_Data.horizontalOffset = 0;
ctl00_ctl00_Menu1_Data.verticalOffset = 0;
WebForm_AutoFocus('ctl00_main_pseudo');// -->
</script>
</form>
</body>
</html>
 
P

PJ on Development

Although JScript docs say that you can use CRLF as command separators,
it's never a good practice because it might let to some errors.

Try to change the code and tell me what happened:

---- BEGIN CODE ----
function pseudoclient(touche)
{
touche = touche ? touche : window.event;
if (touche.keyCode == 13)
document.getElementById("ctl00_main_pseudoSubmit").click();
}
---- END CODE ----

The point with the compairson in the first line is just to help it
work better with FF.

Regards,

Paulo Santos
http://pjondevelopment.50webs.com


Here it is: (maybe i didn't tell it's a content page linked to a
masterpage):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="ctl00_HeadArea"><link href="App_Themes/vert/vert.css"
type="text/css" rel="stylesheet" /><title>
Chatcenter van departement CO
</title><style type="text/css">
.ctl00_ctl00_Menu1_0 {
background-color:white;visibility:hidden;display:none;position:absolute;lef-t:0px;top:0px;
}
.ctl00_ctl00_Menu1_1 { text-decoration:none; }
.ctl00_ctl00_Menu1_2 { }

</style></head>
<body>
<form name="aspnetForm" method="post" action="login3.aspx"
id="aspnetForm">
<div>
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJOTI1MjgxNjUyD2QWAmYPZBYCAgMPZBYEAgEPZBYCZg88KwANAgAPFgIeC18h-RGF0YUJvdW5kZ2QMFCsAAgUDMDowFCsAAhYOHgRUZXh0BQlDaGF0cm9vbXMeBVZhbHVlBQlDaGF-0cm9vbXMeC05hdmlnYXRlVXJsBRAvY2hhdC9zdGFydC5hc3B4HgdFbmFibGVkZx4KU2VsZWN0YW-JsZWceCERhdGFQYXRoBRAvY2hhdC9zdGFydC5hc3B4HglEYXRhQm91bmRnFCsABAULMDowLDA6M-SwwOjIUKwACFg4fAQUKTmV0aXF1ZXR0ZR8CBQpOZXRpcXVldHRlHwMFGi9jaGF0L2NoYXQvbmV0-aXF1ZXR0ZS5hc3B4HwRnHwVnHwYFGi9jaGF0L2NoYXQvbmV0aXF1ZXR0ZS5hc3B4HwdnZBQrAAI-WDh8BBQZVaXRsZWcfAgUGVWl0bGVnHwMFFi9jaGF0L2NoYXQvdWl0bGVnLmFzcHgfBGcfBWcfBg-UWL2NoYXQvY2hhdC91aXRsZWcuYXNweB8HZ2QUKwACFg4fAQUWTGlqc3QgYWN0aWV2ZSBjaGF0d-GVycx8CBRZMaWpzdCBhY3RpZXZlIGNoYXR0ZXJzHwMFFS9jaGF0L2NoYXQvbGlqc3QuYXNweB8E-Zx8FZx8GBRUvY2hhdC9jaGF0L2xpanN0LmFzcHgfB2dkZAIDD2QWAgICDw9kFgIeCm9uS2V5UHJ-lc3MFE3BzZXVkb2NsaWVudChldmVudClkZAAFlsX5toSBBVPhQKrCGHBrHkPy"
/>
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;}

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}}

// -->
</script>

<script
src="/chat/WebResource.axd?d=fR-B6mdb3zMW33vwyvkDdw2&amp;t=6332985973100000-00"
type="text/javascript"></script>

<script
src="/chat/WebResource.axd?d=OqlxnU2t399cVgtpjT19Jg2&amp;t=6332985973100000-00"
type="text/javascript"></script>
<script
src="/chat/WebResource.axd?d=4nnjkKbvtMHy1JuIt0pIFA2&amp;t=6332985973100000-00"
type="text/javascript"></script>
<div id="masterp">
<div id="header1">

<div id="hfd" style="text-align: center">
<font size="6pt">Chatcenter van departement CO</font><br />
<font size="1pt"><br />
philippe mairiaux (c) 2007</font>
<br /><br />
</div>
<div style="height:5px; overflow:hidden"></div>
<!--
overflow:hidden est nécessaire pour IE 6 (pas pour IE 7)
-->
<table id="navig"><tr><td>
<a href="#ctl00_ctl00_Menu1_SkipLink"><img alt="Skip Navigation Links"
src="/chat/WebResource.axd?d=uRPfErkhbPHwfPIGCB4xLg2&amp;t=6332985973100000-00"
width="0" height="0" style="border-width:0px;" /></a><table
id="ctl00_ctl00_Menu1" class="ctl00_ctl00_Menu1_2" cellpadding="0"
cellspacing="0" border="0">
<tr>
<td onmouseover="Menu_HoverRoot(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n0"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/start.aspx">Chatrooms</a></td>
</tr>
</table></td><td style="width:3px;"></td><td><table border="0"
cellpadding="0" cellspacing="0" width="100%">
<tr>
<td onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n1"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/chat/netiquette.aspx"
style="margin-left:16px;">Netiquette</a></td>
</tr>
</table></td><td style="width:3px;"></td><td
onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n2"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/chat/uitleg.aspx" style="margin-left:16px;">Uitleg</a></td>
</tr>
</table></td><td style="width:3px;"></td><td
onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(this)" id="ctl00_ctl00_Menu1n3"><table cellpadding="0"
cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="ctl00_ctl00_Menu1_1"
href="/chat/chat/lijst.aspx" style="margin-left:16px;">Lijst actieve
chatters</a></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table><a id="ctl00_ctl00_Menu1_SkipLink"></a></td></tr></table>

</div>
<div id="maincontent">

<br /><br />
<center>
<fieldset style="width:300px; border-color:#008080">
<br />
Voer een pseudo om mee te chatten!
<h6>(Duw op Enter-toets of klik op de knop)</h6>
<input name="ctl00$main$pseudo" type="text" value="ddd"
id="ctl00_main_pseudo" onKeyPress="pseudoclient(event)" />
<br /><br />
<input type="submit" name="ctl00$main$pseudoSubmit" value="Voer in"
id="ctl00_main_pseudoSubmit" /><br />
<br />
<br />
</fieldset></center>

<script type="text/javascript">
function pseudoclient(touche)
{
var codetouche
touche=event
codetouche=touche.keyCode
if(codetouche == 13)
{
document.getElementById("ctl00_main_pseudoSubmit").click()
}
}

</script>

</div>
</div>

<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
value="/wEWAwKK2ubGCwLrsaj7BAKLte7vBGt9iu7wNVoc9I6/yVRp4lKBTXpU" />
</div>

<script type="text/javascript">
<!--
var ctl00_ctl00_Menu1_Data = new Object();
ctl00_ctl00_Menu1_Data.disappearAfter = 500;
ctl00_ctl00_Menu1_Data.horizontalOffset = 0;
ctl00_ctl00_Menu1_Data.verticalOffset = 0;
WebForm_AutoFocus('ctl00_main_pseudo');// -->
</script>
</form>
</body>
</html>

"PJ on Development" <[email protected]> schreef in bericht

Can you post the actual HTML generated by the ASPX page?

- Show quoted text -
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top