Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

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.

like image 645
Konrad Viltersten Avatar asked Dec 04 '22 15:12

Konrad Viltersten


2 Answers

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;
like image 192
M. Mennan Kara Avatar answered May 18 '23 00:05

M. Mennan Kara


if you want to get value of input use like this

  String s = donkey.value;
like image 25
user1102001 Avatar answered May 18 '23 00:05

user1102001