6

Using the HTML markup

<form id="form" runat="server">
  <input id="donkey" type="text" placeholder="monkey" runat="server" />
</form>

I hoped to get the entered value in code behind by

String s = Request.Form["donkey"];

but it only produces null value. When I investigate the data structure I get something like $ctl00$main$donkey so I know it's there. After a while, I simply switched to

<form id="form" runat="server">
  <asp:TextBox id="donkey" type="text" runat="server"></asp:TextBox>
</form>

but I still wonder how to reference the object from server-side if I for some reason won't switch to ASP-component.

4 Answers 4

7

If you want to access to the value using request.form, add name attribute to input tag and remove runat attribute.

<input id="donkey" name="donkey" type="text" />

Otherwise use

<asp:TextBox ID="donkey" type="text" runat="server"></asp:TextBox>

and on cs

string s = donkey.Text;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It's actually much smoother to use the direct approach of Yograj and User11 but i'll keep the name-attribute in mind. I prefer to use as few as possible in order not to clutter the code.
4

if you want to get value of input use like this

  String s = donkey.value;

Comments

2

I'm not sure about ASP.net but for a regular form field to submit properly it should have a name attribute. That would be the key that you could then lookup the form value.

Comments

2

Just donkey.Value will return the value from text input which should have runat="server". It will create an object of System.Web.UI.HtmlControls.HtmlInputText.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.