Set HTML input textbox's display text using C#

50,895

Solution 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

Solution 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";
Share:
50,895
Piyush
Author by

Piyush

Updated on February 06, 2020

Comments

  • Piyush
    Piyush over 4 years

    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.