473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with referencing a control

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
Oct 10 '07 #1
8 1486
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"/>

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
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


Oct 10 '07 #2
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?
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
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


Oct 10 '07 #3
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


"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:e2*************@TK2MSFTNGP04.phx.gbl...
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?
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>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



Oct 10 '07 #4
oeps, i forgot ...
it's a webapplication
"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:e2*************@TK2MSFTNGP04.phx.gbl...
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?
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>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



Oct 10 '07 #5
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.
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:OC**************@TK2MSFTNGP06.phx.gbl...
oeps, i forgot ...
it's a webapplication
"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:e2*************@TK2MSFTNGP04.phx.gbl...
>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?
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
>>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




Oct 10 '07 #6
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

"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:ec**************@TK2MSFTNGP05.phx.gbl...
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.
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:OC**************@TK2MSFTNGP06.phx.gbl...
>oeps, i forgot ...
it's a webapplication
"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:e2*************@TK2MSFTNGP04.phx.gbl...
>>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?
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
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




Oct 10 '07 #7
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.

--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:e6**************@TK2MSFTNGP04.phx.gbl...
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

"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:ec**************@TK2MSFTNGP05.phx.gbl...
>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.
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:OC**************@TK2MSFTNGP06.phx.gbl...
>>oeps, i forgot ...
it's a webapplication
"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:e2*************@TK2MSFTNGP04.phx.gbl...
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?
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl...
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
>
>




Oct 10 '07 #8
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.

"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
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.

--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:e6**************@TK2MSFTNGP04.phx.gbl...
>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

"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:ec**************@TK2MSFTNGP05.phx.gbl...
>>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.
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Chris" <ch@spam.itwrote in message
news:OC**************@TK2MSFTNGP06.phx.gbl...
oeps, i forgot ...
it's a webapplication
"Mark Fitzpatrick" <ma******@fitzme.comschreef in bericht
news:e2*************@TK2MSFTNGP04.phx.gbl...
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?
>
>
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
>
"Chris" <ch@spam.itwrote in message
news:ee**************@TK2MSFTNGP03.phx.gbl.. .
>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
>>
>>
>
>




Oct 11 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CDWaddell | last post by:
I have the following cod private void btnUpdate_Click(object sender, System.EventArgs e TextBox tbMembers = new TextBox() tbMembers = (TextBox) dlClubs.FindControl("tbMembers") string...
5
by: | last post by:
Hi, I'm trying to use the cookie munging session handling behaviour of asp.net instead of cookies themselves as I'm finding quite a few people are barring cookies (especially AOL users). If I...
5
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
7
by: Martijn Saly | last post by:
Hi there, I've created a master page with some controls on it, a Calendar control among others. Now depending on the date(s) selected, the content page needs to be updated. In the masterpage...
3
by: markh | last post by:
Does anyone know where i could find a good website that explains many techniques for direct control referencing? thanks markh
3
by: gary | last post by:
Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is located in a "/include" folder If you have a...
1
by: yoknows | last post by:
Hello .Net Gurus. This is my first post here so I apologize in advance if I have not provided the right information. I hope someone has seen this problem before and can tell me what I am doing...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
5
by: CCLeasing | last post by:
For an application I'm creating I want to create a 'fake' progress bar. By fake I mean a progress bar that looks like it's doing something but actually isn't. I know philosophically this isn't...
3
topher23
by: topher23 | last post by:
I'm trying to use a WebBrowser control to automate data entry from our local Access system to the web-based corporate ERP system. Unfortunately, I've run into a snag. Several of the text boxes that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.