How to set html input type text value using ASP.NET C#?
Asked Answered
U

5

9

I have an html control which I want to set its value .... here's the control:

<input runat="server" id="first_name_txt" type="text" placeholder="First Name" />

in code behind, I use:

first_name_txt.Value = String.empty;

but the value of my input control still has the old value like "blah-blah" and not set to "".

Ufo answered 12/8, 2014 at 9:45 Comment(4)
I don't get how the value blah-blah got there. I think you are not showing / telling us all.Certiorari
if you used pageload event, see this question [answere][1] [1]: #24918891Jaquez
As @PatrickHofman, stated, show us where the input value is first set to blah-blahGustatory
It's a registration form, and I want to clear the input of first name after clicking button "submit". If I wrote "Andrew" in this input field and pressed "submit", I want the input value to be cleared.Ufo
L
4

Its old question , but may help someone.

You have to use Request.Form to get and call .Value to set the value.

HTML

 <input runat="server" id="first_name_txt" type="text" placeholder="First Name" />

CODE BEHIND

    //To get value:
    string myname=Request.Form["first_name_txt"];

   // To set value:
    first_name_txt.Value="";
Leotaleotard answered 13/1, 2016 at 5:45 Comment(0)
S
3
<td>
  <input type="text" name="date" value="<%= tdate %>" />
</td>

Code Behind :

protected string tdate { get; set; }

 protected void Page_Load(object sender, EventArgs e)
    {
       this.tdate = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
    }
Sundstrom answered 18/8, 2014 at 5:50 Comment(3)
it is working now after I add this control in an update panelUfo
@Ufo if this answer helped you please greenmark it.Fairspoken
I think you wanted to edit your original answer but posted another one instead by mistake.Diaconicum
S
2

Hello it's not that easy to set data into HTML input, but here is a link that may help you [Link].

1) If it didn't work up to you try to set a value and calling it through Javascript and set the text of this input like the gotten value.

2) You can use the [Div] tag using runat="server", clear it and create a new input with same id,name,etc. but different Text value

Try Step 2 as follow(it worked):

   <div id="divTitle" runat="server">
               <input type="text" class="input_Text" id="Title"  name="Title"  /> 
   </div> 

divTitle.Controls.Clear();
divTitle.InnerHtml = "<input type='text' class='input_Text' id='Title'  name='Title' value='" + ds(0)("Title").ToString() + "' />";

Where ds is a data table that came from select query from database

Strawn answered 16/6, 2016 at 12:34 Comment(0)
C
0

Try put this in postback

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           first_name_txt.Value = String.empty;
        }
    }
Camber answered 16/4, 2015 at 11:2 Comment(0)
C
-1

here is very simple way to do this { text_Box.Value = "data";}

Colonial answered 27/3, 2020 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.