Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi
I have a HTML text box (NOT a TextBox server control) in a .aspx page. Is it possible
with ASP.NET to get the value of the HTML text box without declare the element as runat="server"? How can I get the value of an HTML element from C# on the server?
Posted 16-Apr-11 0:15am
Comments
Albin Abel at 16-Apr-11 5:57am
   
It is possible, but Server controls has more flexibility and usability. So if you want simply pass some value from browser to server you can use html else use server control. Look at my answer for your question
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can access the value of the control using Request object. Request["TextBoxID"] should give you the text in the textbox.
 
Other option is - store the TextBox value in a Hidden Field onblur of the TextBox. Then access this hidden field value on the server.
 
Hope this helps!
 
[Edit]Modified the answer as suggested by Albin Abel.[/Edit]
  Permalink  
v2
Comments
Albin Abel at 16-Apr-11 5:56am
   
Ankur everything renders as html (the form tags). And posting is simply a http post. Still we have Request object in asp.Net.
Ankur\m/ at 16-Apr-11 6:03am
   
You are right. The Request object slipped out of my mind. I will update my answer right away.
Thanks a lot!
Albin Abel at 16-Apr-11 6:04am
   
No worries.
Albin Abel at 16-Apr-11 6:47am
   
My 5 for the additional information :)
Ankur\m/ at 16-Apr-11 6:56am
   
Thank you Albin!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I have a HTML text box (NOT a TextBox server control) in a .aspx page. Is it possible= Yes, possible. Nothing different from classical asp in this case. If you don't have runat="server" then you can access it through the old school way.
 
say you have this <input type="text" id="text1" name="text1" /> then from server side use Request["text1"].
 

 
Good luck
  Permalink  
Comments
Ankur\m/ at 16-Apr-11 6:09am
   
And 5 to you! :)
Albin Abel at 16-Apr-11 6:46am
   
Thanks Ankur
Ripon'11 at 16-Apr-11 6:11am
   
thanks... it works
Albin Abel at 16-Apr-11 6:45am
   
Thanks Ripon. Even you can add the runat="server" to html elements as well. Because .Net has a bunch of classes at the System.Web.UI.HtmlControls which can be instantiated at the server for the corresponding html markup. Simply html elements also accepts the runat server attribute. Just an add on information
Ganeshcse at 7-Oct-12 10:04am
   
I have tried the code the same in my asp.net code but it shows an error
"Object reference not set to an instance of an object."
 
What can I do now?
mina mini at 7-Feb-13 7:23am
   
it works,thanks
KARTHIK Bangalore at 21-Jan-14 22:51pm
   
my 5 for you!!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First get the value of the text in javascript
Then send it as a parameter to the serverside method
whatever operations you want, you can do their
It will work well.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

To get the values on server side code of HTML control, we need to follow below points:
 
- The tag should have an attribute called NAME. Because it is used as key in form[].
- The form method should be of type POST.
 
Here is the simple example: http://www.etechpulse.com/2013/02/get-html-input-controls-value-server.html[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

<pre><input name="txtemail" id="txtemail" runat="server" type="text" class="csscla"form="form"/>
 
in C#
string email = txtemail.Value.ToString();
 
now u can get data from input textbox
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

it is not working within
 
I have the following code:
 
<asp:content runat="server" id="BodyContent" contentplaceholderid="MainContent" xmlns:asp="#unknown">
 


<asp:label id="Labelinput" runat="server" associatedcontrolid="datepicker">Input Date:

<input type="text" id="datepicker" name="datepicker" runat="server" clientidmode="static" onchange="mydateChg();" />
<asp:textbox id="TextInputD" runat="server" width="111px" maxlength="10" visible="True" enabled="False" autopostback="True">


<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Save" width="80px" />
 

 

from code behing, I have the following:

protected void Button2_Click(object sender, EventArgs e)
{
 
string strValue = Page.Request.Form["datepicker"];
 
Response.Write(strValue);
}
 

this is retruning null value:
Page.Request.Form["datepicker"];
 

 
if I do the same from another page that does not have
<asp:content runat="server" id="BodyContent" contentplaceholderid="MainContent" xmlns:asp="#unknown">
 
I can get the value of the input control OK
 

any help on this?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 6 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100