Placeholder control

G

Guest

Hi there
I can't believe this is so hard to figure out. I've drawn a placeholder control "phCategory" on my form in asp.net. In my load event I do this

Dim txtCategories As New textbo
phCategory.Controls.Add(txtCategories

The form displays my textbox just fine but once the user clicks submit how do I reference the value of the textbox? Is the textbox I created not a server control where I can access its text property? Do I need to use findcontrol? Thanks in advance!
 
M

Martin Dechev

Hi, Charlie Dison,

You should create the texbox and add it to the controls collection of the
placeholder on every Page_Load, not only when IsPostBack is false.

After that, if you have EnableViewState set to true to both the page and the
textbox, the value will be loaded in the Text property. Note that this value
is loaded after the Page_Load method because the method that loads the
viewstate values to the runtime-generated controls is after the Page_Load
method in the Page class lifecycle.

Then, if you have a handler for the Click event of the button you should be
able to see the user input loaded in the property Text of the textbox. The
only problem is that you don't have a reference to the textbox, so you will
have to find it in the controls collection of the placeholder, and then you
will have to cast it to TextBox in order to get/set the property Text.

Page_Load:

Dim t1 As New TextBox
phCategory.Controls.Add(t1)

Button1_Click:

Dim l1 As New Label
l1.Text = CType(phCategory.Controls(0), TextBox).Text
phCategory.Controls.Add(l1)

Hope this helps
Martin
Charlie Dison said:
Hi there,
I can't believe this is so hard to figure out. I've drawn a
placeholder control "phCategory" on my form in asp.net. In my load event I
do this:
Dim txtCategories As New textbox
phCategory.Controls.Add(txtCategories)

The form displays my textbox just fine but once the user clicks
submit how do I reference the value of the textbox? Is the textbox I
created not a server control where I can access its text property? Do I
need to use findcontrol? Thanks in advance!
 
T

Tommy

There are two ways to access the value of your TextBox.

1. Use the Request.Form("TextBoxId") on a postback
2. Recreate the TextBox again, and access the .Text property

Dim txtCategories As New textbox
phCategory.Controls.Add(txtCategories)
txtCategories.Text

Tommy,
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top