简体   繁体   English

如何使用 ASP.NET C# 设置 html 输入类型文本值?

[英]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:我有一个 html 控件,我想设置它的值......这是控件:

<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 "".但是我的输入控件的值仍然具有旧值,例如“blah-blah”并且未设置为“”。

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

Its old question , but may help someone.它的老问题,但可能会帮助某人。

You have to use Request.Form to get and call .Value to set the value.您必须使用Request.Form来获取和调用.Value来设置值。

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

Hello it's not that easy to set data into HTML input, but here is a link that may help you [Link] .您好,将数据设置为 HTML 输入并不容易,但这里有一个链接可以帮助您[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 . 1)如果它不起作用,您尝试设置一个并通过Javascript调用它并将此输入的文本设置为获取

2) You can use the [Div] tag using runat="server", clear it and create a new input with same id,name,etc. 2) 您可以使用 runat="server" 使用 [Div] 标签,清除它并创建一个具有相同 ID、名称等的新输入。 but different Text value但不同的文本值

Try Step 2 as follow(it worked):尝试步骤 2 如下(它有效):

   <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其中 ds 是来自数据库中选择查询的数据表

Try put this in postback尝试将其放在回发中

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

Try this Code Behind: 尝试以下代码:

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

HTML: HTML:

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

Just tried it, WORKS! 刚刚尝试过,工作!

这是执行此操作的非常简单的方法 { text_Box.Value = "data";}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM