Konrad Viltersten
Konrad Viltersten

Reputation: 39278

How to obtain a value of an input (type text) on server side in ASP.NET?

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.

Upvotes: 6

Views: 35663

Answers (4)

Yograj Gupta
Yograj Gupta

Reputation: 9869

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.

Upvotes: 2

M. Mennan Kara
M. Mennan Kara

Reputation: 10222

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;

Upvotes: 7

user1102001
user1102001

Reputation: 707

if you want to get value of input use like this

  String s = donkey.value;

Upvotes: 4

scunliffe
scunliffe

Reputation: 63676

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.

Upvotes: 2

Related Questions