Reputation: 12709
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"
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"
Upvotes: 1
Views: 2402
Reputation:
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
.
Upvotes: 1
Reputation: 13266
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
Upvotes: 1