problem with referencing a control

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"></textarea>
</fieldset>
</asp:Content>


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


How can i reference the textarea?
Thanks
Chris
 
A

Aidy

You need runat="server"

<textarea id="txta" rows="6" runat="server"></textarea>

However I'd suggest you use an asp:TextBox instead but set it to multiline
mode

<asp:TextBox ID="txta" runat="server" TextMode="MultiLine" Rows="6"/>
 
M

Mark Fitzpatrick

Before you get to the line ta = mp.FindControl("txta") you need to test to
see if the object mp is null or not. Always check to see if the object is
null before you attempt to access it.

If you're doing this on code-behind though, you shouldn't have to do a
findcontrol to get Content1. It should already be accessible. Is this a Web
Site Project or a Web Application Project?
 
C

Chris

Thanks both for replying.
1) i use textarea instead of asp:textbox because i need the ONKEYPRESS
event, which is not available with textbox.
2) doing runat="server" for the textarea allows me indeed to put the focus
straightfully like this: txta.focus(), but then, when pressing Return on the
keyboard, i get a javascript error.
3) when typing in code-behind: content1. the intellisense shows nothing and
i get the error: "name is not declared".
When doing:
Dim mp As Content
mp = FindControl("Content1")

no error and it works ...


My purpose is to enter text in the textarea and send it to the database with
the Return key. I also want the focus be put in that textarea.

Sofar, i have to choose between having the focus on textarea or the
Onkeypress event. Anyway to get both of them together?
Thanks
 
C

Chris

oeps, i forgot ...
it's a webapplication


Mark Fitzpatrick said:
Before you get to the line ta = mp.FindControl("txta") you need to test
to see if the object mp is null or not. Always check to see if the object
is null before you attempt to access it.

If you're doing this on code-behind though, you shouldn't have to do a
findcontrol to get Content1. It should already be accessible. Is this a
Web Site Project or a Web Application Project?
 
M

Mark Fitzpatrick

Is there a .designer file being generated for the page? The Web Application
Project creates a .designer file that has the variables defined in them so
you shouldn't need to do anything else unless the file is not being
generated. I've seen instances of this not occuring if there's an issue in
the web.config file. Also, make sure you open your web page and cycle from
HTML view to Design View. This usually forces it to parse the page and
create the designer file with the definitions.

If the designer is working correctly you shouldn't be doing any of this.
There's no reason to use FindControl in this situation as something else has
broken. It doesn't have anything to do with the HTML area since the main
problem is you aren't actually getting access to it to begin with since your
object null error is actually happening when you try to access the content
object as mp.FindControl. If the mp object is null it will throw this error.
 
C

Chris

Very sorry, Mark, I was wrong: it's a web site project (done with VWD).
I you have a solution for my choice: focus or onkeypress ...
Thanks again
 
M

Mark Fitzpatrick

Ok, still, something is wrong here. Your focus and onkeypress isn't an
issue. Your designer should still be finding the control in VWD.

Have you tried ignoring intellisense and just checking to see if it runs? I
mean, just try txta.Focus() and see what happens. Don't use the FindControl.
It could be that it will actually run, but for some reason intellisense
isn't updating. I've had that issue before as well. Sometimes I just plow
forward and it works regardless of what the intellisense is saying.

Couple things to look for, make sure the CodeFile declaration is correct in
the .aspx page. The class should also be a partial class for the code file
(I don't remember if there is a special VB syntax or not since I'm used to
C#) so that the .vb code page is determined to be in the same class space as
the .aspx page.
 
C

Chris

Mark,
I think it makes no sense to do: txta.Focus() because there is no
run="server" in its tag (otherwise the onkeypress is no longer available),
so it can't be recognized in code-behind.
What i did is:

Dim ta As HtmlTextArea
ta = Master.FindControl("txta")
ta.Focus()

Here, the Intellisense gives the focus method, but when running it; i get
the same error at line: ta.Focus()

I'm sure that the codeFile declaration is correct in the .aspx page and that
the class is a partial class for the code file.That's not the point.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top