Set Focus on a TextBox

N

Nicolas Haenen

Hello,

I'm developping a Barcode Scanning program on ppc. I need to set focus on a textbox at page load (and so scan directly... with no use of the mouse to go in the textbox before).

I tried this but don't works :

public void setFocus(System.Web.UI.MobileControls.MobileControl champ

string s
s = "<script language= \"javascript\">document.getElementById('" +
champ.ClientID + "').focus()</script>";
Page.RegisterClientScriptBlock("Focus", s)


private void Page_Load(object sender, System.EventArgs e

if(Page.IsPostBack==false

this.setFocus(Field_Interet)



Thanks a lot for help, Nicolas
 
J

Jim Cheshire [MSFT]

Hi Nicolas,

RegisterClientScriptBlock will place your script before the <form> tag on
the page. Therefore, when this script is encountered, the TextBox to which
you are trying to set focus has not yet been rendered. What you want to do
is use RegisterStartupScript instead. That will place your script after
the closing </form> tag and will work correctly.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.


--------------------
Thread-Topic: Set Focus on a TextBox
thread-index: AcQYuNdR+rtRvI2kQBmNDWGmskT8KQ==
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.mobile
From: "=?Utf-8?B?Tmljb2xhcyBIYWVuZW4=?="
Subject: Set Focus on a TextBox
Date: Fri, 2 Apr 2004 05:46:03 -0800
Lines: 24
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.aspnet.mobile
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.mobile:6977
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.mobile

Hello,

I'm developping a Barcode Scanning program on ppc. I need to set focus on a
textbox at page load (and so scan directly... with no use of the mouse to
go in the textbox before).


I tried this but don't works :

public void setFocus(System.Web.UI.MobileControls.MobileControl champ)
{
string s;
s = "<script language= \"javascript\">document.getElementById('" +
champ.ClientID + "').focus()</script>";
Page.RegisterClientScriptBlock("Focus", s);
}

private void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack==false)
{
this.setFocus(Field_Interet);
}
}

Thanks a lot for help, Nicolas
 
N

Nicolas HAENEN

Hi,

This code don't work
public void setFocus(MobileControl champ

string s
s =
"<script language=javascript> "
" document.getElementById('" + champ.ClientID + "').focus(); "
"</script>"

Page.RegisterStartupScript("Focus", s)


Do you know another way to resolve my problem ?

Thanks, Nicolas
 
J

Jim Cheshire [MSFT]

Nicolas,

Your string contains a newline character as far as C# is concerned. That
may be why it's not working for you. I suspect that this would generate a
compile error. You should also use a StringBuilder for this instead of a
string.

Here's a code sample to show you what I mean:

<%@ Page Language="C#" %>
<script runat="server">

void Page_Load(object sender, EventArgs e) {
System.Text.StringBuilder sb = new System.Text.StringBuilder("");

sb.Append("<script language='JavaScript'>");
sb.Append("document.getElementById('" + TextBox1.ClientID +
"').focus();<");
sb.Append("/");
sb.Append("script>");

if (!IsStartupScriptRegistered("Focus"))
{
this.RegisterStartupScript("Focus", sb.ToString());
}
}

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>


Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
Thread-Topic: Set Focus on a TextBox
thread-index: AcQbueChKW5u221IQUWCGSftUuSppQ==
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.mobile
From: "=?Utf-8?B?Tmljb2xhcyBIQUVORU4=?="
Subject: RE: Set Focus on a TextBox
Date: Tue, 6 Apr 2004 02:31:02 -0700
Lines: 18
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.aspnet.mobile
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.mobile:6987
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.mobile

Hi,

This code don't work :
public void setFocus(MobileControl champ)
{
string s;
s =
"<script language=javascript> "+
" document.getElementById('" + champ.ClientID + "').focus(); "+
"</script>";

Page.RegisterStartupScript("Focus", s);
}


Do you know another way to resolve my problem ?

Thanks, Nicolas
 

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

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top