Set HTML input textbox's display text using C#

拥有回忆 提交于 2019-12-23 19:17:10

问题


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

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

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.


回答1:


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.

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

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.

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

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




回答2:


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



回答3:


Do this:

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

The msdn claims that the following will work :

<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!



来源:https://stackoverflow.com/questions/9148161/set-html-input-textboxs-display-text-using-c-sharp

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