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

十年热恋 提交于 2019-12-06 02:47:58

问题


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


回答1:


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="";



回答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




回答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();
    }



回答4:


Try put this in postback

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           first_name_txt.Value = String.empty;
        }
    }



回答5:


Try this Code Behind:

protected string tdate { get; set; };
this.tdate = "your value";

HTML:

<input type="text" name="date" value="<%= tdate %>" 

Just tried it, WORKS!



来源:https://stackoverflow.com/questions/25261138/how-to-set-html-input-type-text-value-using-asp-net-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!