1

I am trying to post data from one page to another by using an ASP TextBox control, but I cannot read its name attribute because it gets mangled by the master page. (or so I understand)

If I set the ID of the TextBox to TextBox1, then its name will be something like ctl00$BodyContent$TextBox1 which prevents me from reliably reading the POST data by simply using its name.

How should I go about resolving this?

Should I use a simple HTML input tag instead? I would rather not, because it is obviously less flexible.

Acidic
  • 6,154
  • 12
  • 46
  • 80

2 Answers2

2

User Control.ClientIDMode property to static.
Here is a msdn Example
For better understanding here is another example

शेखर
  • 17,412
  • 13
  • 61
  • 117
  • I've just tested this, and it seems to prevent the `id` from being scrambled - but not the `name` attribute; which is what I need to use to retrieve the correct POST data. – Acidic Oct 25 '12 at 05:15
  • asp.net works on ID rather than Name attribute. Can you provide me your code. There are many different ways of getting data on next page. Like previous page directive here is an example. http://msdn.microsoft.com/en-us/library/ms178139%28v=vs.100%29.aspx – शेखर Oct 25 '12 at 05:26
  • The Page directive do not semm to work for my code, as the `Page` property is always `null`.I am using an `asp:Button` with with the `PostBackUrl` property to submit the form to another page. I am trying to acess the POST data in the following manner - `HttpContext.Current.Request.Form["name_of_element"]`, which requires the name of the element rather than its ID. – Acidic Oct 25 '12 at 05:34
  • Your are confused with PHP and Asp.net PHP works on "name" attribute but asp.net works on "ID" attribute. – शेखर Oct 25 '12 at 05:35
  • Well, the only thing present in the `AllKeys` collection of the form's is the name of the TextBox (and a few other unrelated things), not it's ID: `ctl00$BodyContent$TextBox1`. Or am I doing something wrong? – Acidic Oct 25 '12 at 05:39
  • frankly speaking I have not worked on request.form.allkeys collection. Still if you want to wrok on AllKey collection only then you will have to use some kind of string function like substring or regular expression which will extract the exact name from the collection. You will see that there is a certain pattern of naming the controls "ctl00$BodyContent$" is fixed in your all control remove these from and get the value. – शेखर Oct 25 '12 at 05:59
  • here is a example which may also help you http://stackoverflow.com/questions/7897445/can-i-force-asp-to-set-name-the-same-as-id – शेखर Oct 25 '12 at 06:05
  • I want to use the `Request.Form` collection, the `AllKeys` property simply allowed me to check which keys are actually present in the collection. Anyway, I thought about using substring but it seemed like an inelegant way of doing something so trivial. Is there really no simple way to just send/read POST requests in ASP.NET? It is so extremely easy in PHP... :/ – Acidic Oct 25 '12 at 06:10
  • I know that it is difficult to understand that why we can't get the posted values on other page as compare to PHP but the difference is in the architecture of the PHP and Asp.net. There is a method "server.transfer" your should explore this. This can help you. – शेखर Oct 25 '12 at 06:36
  • Well, for now I guess I'll juts go with string manipulation... This is what I came up with to get the key - - - `var key = form.AllKeys.First(x => x.EndsWith("TextBox1"));`. Works for now and seems pretty concise. Too bad we couldn't find an optimal solution, but thanks for the help regardless. – Acidic Oct 25 '12 at 10:15
  • 1
    My pleasure. I will look for the better solution. – शेखर Oct 25 '12 at 11:01
0

You can get its ID at the client end.

<%= TextBox.ClientID %>

The above will give you the mangled ID. :)

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • wouldn't that force me to use Javascript? – Acidic Oct 25 '12 at 05:06
  • Else, You could pass this too as a param to your other page, in your POST. But sounds too complicated, and unnecessarily so. – Anirudh Ramanathan Oct 25 '12 at 05:08
  • It just seems strange to me that something as trivial as this is so hard to achieve in ASP.NET compared to PHP. If I switch to the simple HTML input tag, it becomes as easy as this - `HttpContext.Current.Request.Form["TextBox1"]` - Why can't I achieve this simplicity with the `asp:TextBox` control? – Acidic Oct 25 '12 at 05:11