1

I'm trying to submit the below form where a master page is applied.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <p>
      What is your name?
      <asp:TextBox ID="YourName" runat="server" ClientIDMode="Static"></asp:TextBox>
   </p>

   <p>
      What is your age?
      <asp:TextBox ID="YourAge" runat="server" Columns="3" ClientIDMode="Static"></asp:TextBox>
   </p>

   <p>
      What is your favorite color?<br />
      <asp:RadioButtonList ID="FavoriteColor" runat="server" ClientIDMode="Static">
         <asp:ListItem Selected="True">Red</asp:ListItem>
         <asp:ListItem>Green</asp:ListItem>
         <asp:ListItem>Blue</asp:ListItem>
      </asp:RadioButtonList>
   </p>

   <p>
      <asp:CheckBox ID="EatGreenEggsAndHam" runat="server" Checked="True" ClientIDMode="Static" 
      Text="Will You Eat Green Eggs and Ham?" TextAlign="Left" />
   </p>

   <p>
      <asp:Button ID="SubmitButton" runat="server" Text="Submit Form" 
           PostBackUrl="~/About.aspx" 
           onclick="SubmitButton_Click" />
   </p>

</asp:Content>

I'm trying to use POST method to get submitted data from the form.

in About.aspx page i'm using the below code

Response.Write(Request.Form["YourName"].ToString());

expected behavior is to print the name typed in textbox with the id "YourAge" but the submitted form element ids have changed like below

ctl00$MainContent$YourName

why does this happen? i need to get the value of the control with the id "YourName"

enter image description here

I want to remove this "ctl00$MainContent$" part when submitting form. retrieving the form values are out of my scope and it's done by some other people. They only require form values to be submitted as "YourName" not "ctl00$MainContent$YourName"

chamara
  • 12,649
  • 32
  • 134
  • 210

2 Answers2

1

Try to utilise the abstraction that ASP.net provides. You can use YourName.Text to retrieve the value of the YourName TextBox.

Response.Write(YourName.Text);

Also, ctl00$MainContent$YourName is not the ID of the element generated, but its name. The ID will probably be something like BodyContent_YourName.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • As the chamara is using ASP.NET 4.0 Client ID mode as static, the id rendered would just be "YourName" – Ramesh Oct 22 '12 at 08:12
  • Where does it say he is using ASP.NET 4.0 with Client ID mode static? – Asad Saeeduddin Oct 22 '12 at 08:14
  • ClientIDMode="Static" is a ASP.NET 4.0 feature. ref: http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40 – Ramesh Oct 22 '12 at 08:15
  • @chamara ClientIDMode affects only the ID attribute of the input element. The `Request.Form` would contain the name attribute of the input element. Hence it is not working as expected. – Ramesh Oct 22 '12 at 08:26
1

In the first place, it is not advisable to directly read from the Request.Form. Most of the time You should be using the YourName.Text to read the value.

But, if you would still like to read the value from Request.Form you need to understand how the ASP.NET pipe line and http protocol works. When you submit a page from browser, browser would create a HTTP Request with key value pairs, where the key denotes the "name" of the input control while the value denoted the corresponding value.

In your code you are only setting the ID to be static but not the name. So, to set the name same as ID, you can use the techniques described for this SO Question

Udpate

Another approach I could think of is as below

Response.Write(Request.Form[YourName.UniqueID].ToString());

The Unique ID is the name which is rendered to the element and you can use the same for fetching the values from the Request.Form

Community
  • 1
  • 1
Ramesh
  • 13,043
  • 3
  • 52
  • 88