how to put focus on textarea in content page?

C

Chris

Hi,

i have in an content page a fieldset containing a label, an iframe and a
textarea:

<asp:Content ID="Content1" ContentPlaceHolderID="main" Runat="Server">
<fieldset style="width:650px;">
<asp:Label ID="Label1" runat="server"></asp:Label>
<iframe ..... ></iframe>
<textarea id="txta" rows="6" onkeypress="return
mysub(event)"></textarea>
</fieldset>
</asp:Content>

In order to send the text, the user must press the ENTER key.
What i'm trying to do is to give the focus to the textarea.


I did this in code-behind:

Dim ta As HtmlTextArea
Dim mp As Content

mp = FindControl("Content1")
ta = mp.FindControl("txta")
ta.Focus()

but this gives the error: "Object reference not set to an instance of an
object. " for line ta = mp.FindControl("txta")
(I use textarea instead of asp:textbox because i need the ONKEYPRESS event,
which is not available with textbox).

I then tried with runat="server" in the textarea tag and that allows me to
put the focus straightfully like this: txta.focus(), but then, when pressing
Return on the keyboard, i get a javascript error (because the onkeypress
event is no longer available).

So, i have to choose between having the focus on textarea or the Onkeypress
event.

Anyway to get both of them together?

Thanks Chris
 
C

Cor Ligthert[MVP]

Lou in my idea is that impossible to do in the way you want it.



The textbox is processed on client side the code behind on serverside.



This is the only way that works for me.

<INPUT tabindex="1" id="textbox1" type="text" size="27">


//This should be the last rows in your html aspx file
(Look for the id in the Input row in the aspx html part.)


<script language="JavaScript">
document.all("textbox1").focus();
</script>
</HTML>


Cor
 

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

Top