How to get input value (text type) on server side in ASP.NET?

Using HTML markup

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

I was hoping to get the entered value in the code behind

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

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

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

but I'm still wondering how to refer to the object from the server side, if for some reason I donโ€™t switch to the ASP component.

+4
source share
4 answers

if you want to get the input value, for example,

  String s = donkey.value; 
+5
source

If you want to access the value using request.form, add a name attribute to enter the tag and remove the 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; 
+6
source

just donkey.Value will return a value from text input that has runat = "server". since it will create a System.Web.UI.HtmlControls.HtmlInputText object.

+2
source

I'm not sure about ASP.net, but for the form field to fill correctly, it must have a name attribute. This will be the key to finding the meaning of the form.

+2
source

All Articles