Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 "".

like image 671
Andrew_Nady Avatar asked Aug 12 '14 09:08

Andrew_Nady


People also ask

How to get value of HTML input control in ASP net c#?

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.

How do you style the value of an input?

Styling Input Fields If you only want to style a specific input type, you can use attribute selectors: input[type=text] - will only select text fields. input[type=password] - will only select password fields. input[type=number] - will only select number fields.

What is Aspnetform?

❮ Previous Next ❯ A form is a section of an HTML document where you put input controls (text boxes, check boxes, radio buttons, and pull-down lists).


3 Answers

<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();
    }
like image 166
Kousick Shanmugam Nagaraj Avatar answered Sep 22 '22 14:09

Kousick Shanmugam Nagaraj


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="";
like image 40
Anoop B.K Avatar answered Sep 21 '22 14:09

Anoop B.K


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

like image 23
AhmadMM Avatar answered Sep 19 '22 14:09

AhmadMM