0

I have an ASP.NET web app where whenever the user chooses an item from a particular dropdown list a number of controls will get updated accordingly, for example labels will get renamed, textboxes will have their id changed etc. To do this i'm using jquery.

After the textboxes are given a new id depending on what the user chooses, i would like to get the values inputted on those textbox passed to a method, i tried doing Request.Form[controlid] however it returns null. I thought this was happending because i was using asp.net controls, so i decided to create the textboxes dynamically using jquery..however it would still return null.

i also tried doing something like:

TextBox textBox = Page.FindControl(controlid) as TextBox

textBox.Text;

however it would still return me null.

Any ideas please?

anouar.bagari
  • 2,084
  • 19
  • 30
krafo
  • 183
  • 5
  • 20

4 Answers4

0

Page.FindControl only finds controls directly in the page, it is not recursive. If you have controls in a Panel or some other control, it won't work.

See this post:

Better way to find control in ASP.NET

Community
  • 1
  • 1
Rob
  • 5,578
  • 3
  • 23
  • 28
0

You need to use Request.Form[controlname] instead of controlid.

The server side FindControl won't work as it will only recognize controls that already exist server side.

LouD
  • 3,776
  • 4
  • 19
  • 17
0

When the form is submitted, it is the name attribute that is the key in the Request.Form like this Request.Form["controlname"] Because you're dynamically changing the textboxes on the client side using javascript, ASP.NET will not be able to find them using Page.FindControl(). On the client side, you'll have textboxes like this:

<input type="textbox" id="t1" name="t1name" />

The value of this will be available as Request.Form["t1name"]

truemedia
  • 1,042
  • 6
  • 12
0

You need to add the attribute

runat="server"

on your control, on the .aspx page.

Then use [ControlName].Text on the CodeBehind to find the current text corresponding to your control.

Pouki
  • 1,654
  • 12
  • 18