使用 C# 设置 HTML 输入文本框的显示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9148161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:10:08  来源:igfitidea点击:

Set HTML input textbox's display text using C#

c#asp.nethtmlhtml-input

提问by Piyush

I have a HTML input box in my ASPX page like below

我的 ASPX 页面中有一个 HTML 输入框,如下所示

<input id="txtID" runat="Server" type="text" />

Now I have some code written in C# codebehind which calculate a value and I want that value to be displayed in the above TextBox.

现在我有一些用 C# codebehind 编写的代码来计算一个值,我希望该值显示在上面的 TextBox 中。

I have already tried

我已经试过了

txtID.Value = Number.ToString();

and

HtmlGenericControl ct = new HtmlGenericControl();
ct.InnerHTML = Number.ToString();
txtID.Controls.Add(ct);

but both of the above does not seems to set the display text of the textbox.

但以上两者似乎都没有设置文本框的显示文本。

Can anyone help me figure out as to how do I get it done. I cannot use

谁能帮我弄清楚如何完成它。我不能使用

<asp:TextBox />

EDIT (WITH CORRECT ANSWER):The way I was originally trying to do was correct i.e.

编辑(有正确答案):我最初尝试做的方式是正确的,即

txtID.Value = Number.ToString();

The culprit was Placeholder Plugin which was getting called and was erasing the values from the TextBox. Hope this will help a lot of people like me who get stuck at such silly places.

罪魁祸首是占位符插件,它被调用并从文本框中删除值。希望这能帮助很多像我一样被困在这些愚蠢地方的人。

采纳答案by Medeni Baykal

You can change value of control by injecting Javascript on PageLoad or PageInit. Just say GetValueDummy()method is your method to calculate a value and you are using jQuery.

您可以通过在 PageLoad 或 PageInit 上注入 Javascript 来更改控件的值。只是说GetValueDummy()方法是您计算值的方法,并且您正在使用jQuery。

You need to inject a javascript to page in Page.Load handler.

您需要在 Page.Load 处理程序中向页面注入 javascript。

protected void Page_Load(object sender, EventArgs e)
{
    var script = "$('#txt').val('" + GetValueDummy() + "');";
    ClientScript.RegisterStartupScript(typeof(string), "textvaluesetter", script, true);
}

In this code, txt is id of your input.

在此代码中,txt 是您输入的 ID。

If you are not using jQuery just replace value of script variable to

如果您不使用 jQuery,只需将脚本变量的值替换为

var script = "document.getElementById('txt').value = '" + GetValueDummy() + "';";

After some point your page will be fully rendered and ready to send to client. So you cant directly modify it from c#. You can read more about page life time here: http://msdn.microsoft.com/en-us/library/ms178472.aspx

在某个时间点后,您的页面将完全呈现并准备好发送给客户端。所以你不能直接从 c# 修改它。您可以在此处阅读有关页面生命周期的更多信息:http: //msdn.microsoft.com/en-us/library/ms178472.aspx

回答by Mithrandir

Do this:

做这个:

<input type="text" ID="txtID" runat="server" />

The msdnclaims that the following will work :

MSDN声称,下面的工作:

<input 
    Type="Password|Text"
    EnableViewState="False|True"
    Id="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnServerChange="OnServerChange event handler"
    OnUnload="OnUnload event handler"
    runat="server"
    />

if it "doesn't help", the problem might not be with the markup!

如果它“没有帮助”,则问题可能不在于标记!

回答by coder

Give it like this:

像这样给它:

<input type="text" name="email" id="MyInput" runat="server" />

Access it like this:

像这样访问它:

string  MyInput= myTextBox.Value;

Sorry for the above answer:

抱歉上面的回答:

Here is the Edit:

这是编辑:

this.Init += Page_Init;
this.Load += Page_Load;
protected void Page_Init(object sender, System.EventArgs e)
{
        createControls();
    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (IsPostBack)
        {
            setcontrolvalues();
        }
    }

    private void createControls()
    {
        TextBox txt1 = new TextBox();
        TextBox txt2 = new TextBox();
        txt1.ID = "txt1";
        txt1.EnableViewState = true;
        txt2.EnableViewState = true;
        txt2.ID = "txt2";
        PlaceHolder1.Controls.Add(txt1);
        PlaceHolder1.Controls.Add(txt2);
    }

    private void setcontrolvalues()
    {
        TextBox txt1 = null;
        TextBox txt2 = null;
        txt1 = (TextBox)(PlaceHolder1.FindControl("txt1"));
        txt1.Text = "text1";
        txt2 = (TextBox)(PlaceHolder1.FindControl("txt2"));
        txt2.Text = "text2";