Click here to Skip to main content
15,916,318 members
Please Sign up or sign in to vote.
4.20/5 (4 votes)
See more:
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
Updated 2-Mar-23 0:18am
Comments
Albin Abel 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

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]
 
Share this answer
 
v2
Comments
Albin Abel 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/ 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 16-Apr-11 6:04am    
No worries.
Albin Abel 16-Apr-11 6:47am    
My 5 for the additional information :)
Ankur\m/ 16-Apr-11 6:56am    
Thank you Albin!
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
 
Share this answer
 
Comments
Ankur\m/ 16-Apr-11 6:09am    
And 5 to you! :)
Albin Abel 16-Apr-11 6:46am    
Thanks Ankur
Ripon'11 16-Apr-11 6:11am    
thanks... it works
Albin Abel 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
Ganesh KP 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?
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.
 
Share this answer
 
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[^]
 
Share this answer
 
ASP.NET
<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
 
Share this answer
 
v2
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?
 
Share this answer
 
Comments
IrcdaemonZ 18-Jun-19 1:57am    
try to use below syntax....you cannot get value of input text directly if it's inside a masterpage or contentplaceholder. You need to find the object first and get the value.


ContentPlaceHolder test = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

((HtmlInputText)test.FindControl("TextInputD")).Value.ToString()
You cannot get the value of the input text directly since its inside a masterpage and contentplaceholder.

You need to get the Object from the contentplaceholder in Masterpage and then get the value from the object as below.

ContentPlaceHolder test = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
var value= ((HtmlInputText)test.FindControl("txtEmail")).Value.ToString()
 
Share this answer
 
Comments
Maciej Los 18-Jun-19 2:12am    
This question has been already answered (accepted answer). Why to post another answer?
You cannot get the value of the input text directly since its inside a masterpage and contentplaceholder.

You need to get the Object from the contentplaceholder in Masterpage and then get the value from the object as below.

ContentPlaceHolder test = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
var value= ((HtmlInputText)test.FindControl("txtEmail")).Value.ToString()..
 
Share this answer
 
Comments
CHill60 18-Jun-19 5:10am    
Please do not post the same thing twice. Your post could have been waiting for a moderator to clear its publication. Be patient.
in my case

var value= ((HtmlInputText)test.FindControl("txtEmail")).Value.ToString()..
this line shows error of conversion

I changed the HtmlInputText to HtmlInputControl .

//source
<ItemTemplate>
      <tr>
      <td class="text-center"><asp:CheckBox ID="chkadon" runat="server" /></td>
      <td class="w-50"><asp:Label ID="lbadonname" runat="server" Text= '<%#Eval("addonName") %>'>
        </asp:Label>   </td>
       <td><input runat="server" id="advalue" type="number" min="1" max="10" value="1"> </td>
      </tr>
     </ItemTemplate>


//code behind
string ss;
          string advalue;
foreach(RepeaterItem rpt in rptaddon.Items)
        {
            CheckBox chkap= (CheckBox)rpt.FindControl("chkadon");
            Label lbadonname = (Label)rpt.FindControl("lbadonname");
            HtmlInputControl adqty = (HtmlInputControl)rpt.FindControl("advalue");

            if (chkap.Checked)
            {
                ss=lbadonname.Text;
                advalue= adqty.Value;

            }
        }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900