Referencing HTMLTextArea

W

Will T

I have added an HTMLTextArea object (htaNote) to my
webform.aspx and in the Page_Load codebehind I type
something like:

htaNote.Value = "Ouch!"

Unfortunately, the intellisense does not provide
the .Value for me because it does not recognize the object.

I cannot reference Me.htaNote, either.

If I:
Dim hta As HTMLTextArea

I can:
hta.Value (with Intellisense)

so I tried:
Dim hta As HTNLTextArea
hta = Me.FindControl("htaNote")
hta.Value = "Ouch!"

but this fails since it doesn't find an htaNote object on
the form.

What do I need to reference this object?
Any help would be appreciated.
 
K

Kevin Spencer

The FindControl Method looks for only those Controls which are immediate
children of the Control referenced. Since the HtmlTextArea is inside your
WebForm, and the WebForm is a child of the Page, you will not find it in the
controls Collection of the Page, but of the WebForm. Therefore, try:

hta = Me.FindControl("Form1").FindControl("htaNote")

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
W

Will T

Your answer makes perfect sense, however I cannot make it
work.

the code:
hta = Me.FindControl("Form1").FindControl("htaNote")

still fails to find an object. The form name is "Form1",
just to make things clear.

Error: Object reference not set to an instance of an
object.
 
K

Kevin Spencer

Well, you were using a rather unusual method of declaring your Control; I
wasn't sure whether it would work. What you need to do is to declare the
Control in your CodeBehind class as a field of the Page class:

Protected htaNote As System.Web.UI.HtmlControls.HtmlTextarea

That will wire it up to the Control in the Template. Then you can reference
it and work with it in your Page_Load Sub. Assuming that the id of the
Control in the Template matches the name of the Control in the declaration.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
W

Will T

Again, your logic is solid and looks like it should work.

I added:
Protected htaNote As
System.Web.UI.HtmlControls.HtmlTextarea

in the #Region bewteen "Private Sub InitializeComponent()"
and "Private designerPlaceholderDeclaration As
System.Object"

so my code in #Region now reads:
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected htaNote As
System.Web.UI.HtmlControls.HtmlTextArea

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web
Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

This allows me to have intellisense on htaNote, however I
still get the same error when I run the code.

I find it odd that .NET does not add the "Protected
htaNote..." itself, as it does for other objects.

Still lost,
Will
 
W

Will T

BTW, I have also placed the "Protected htaNote..." above
the #Region, with the same results.

-Will
 
K

Kevin Spencer

What software are you using to develop with? Visual Studio.Net will add the
property declaration for you when you either drag a Control onto the Page
template or make a Control "runat=server".

What does the tag in the Page Template look like? It has to be some problem
with the correspondence between the tag in the Template and the property
declaration. the property declaration is correct. And I'm assuming you took
the Dim statement out.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
W

Will T

Firstly, thanks for the help you are providing.

I am developing in VS.NET 2003. To reproduce this
situation, follow these steps:

1) Add a Webform to a web project (WebForm1.aspx)
2) In the Toolbox, click the HTML tab.
3) Drag an HTMLTextArea object to the screen.
4) Open it's properties and name it "DOG" (id="DOG")
5) Open the Code view
6) Perform a search for "DOG".

Mine yields no results.

7) Open the HTML view
8) Search for "DOG" again.

Mine finds the object:
<TEXTAREA id="dog" style="Z-INDEX: 103;
LEFT:24px;POSITION: absolute; TOP: 80px" rows="2"
cols="20"></TEXTAREA>

No reference in the webform, but one in the HTML. I am
lost.

-Will
 
K

Kevin Spencer

Ah, the light is coming on now!

If you're dragging an HTML TextArea from the ToolBox, you are not creating
an HTMLControl, you are creating an HTML Textarea. To have it become an
HTMLControl, you can right-click it in the Designer, and select "run as
server control" from the context menu. Then the IDE will create the
CodeBehind Reference.

Alternatively, you can add a "runat=server" attribute in the HTML View,
assign an ID to it, and switch back to Designer view. When you switch back
to Designer view, it adds the CodeBehind for the Control.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 
W

Will T

Jiminy Crickets! It worked! I owe you lunch.

I was leary about making the objects "Run at Server". You
see, the whole point of this is that I need a text field
and a counter. As the user types in the text field, the
counter needs to reflect the number of characters typed. I
am doing this via javascript.

Using an HTMLTextArea, I was able to do this. The catch
was that I could not preload any data into the
HTMLTextArea (which is what you helped me resolve).

My fear was that by making the object "Run at Server" it
would require a postback with each character typed. On the
development machine, this has not been the case. I will
try to deploy the app and test it via the web, for the
real test.

Thanks again,
Will
 
K

Kevin Spencer

Exzcellent, Dude!
My fear was that by making the object "Run at Server" it
would require a postback with each character typed. On the
development machine, this has not been the case. I will
try to deploy the app and test it via the web, for the
real test.

No problem there. An HtmlTextArea Control doesn't have any server-side
events by default. In fact, I think you'd have to jump through some hoops to
make it generate one.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.
 

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,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top