3

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.

Piyush
  • 886
  • 3
  • 9
  • 28

3 Answers3

3

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

Medeni Baykal
  • 4,223
  • 1
  • 28
  • 36
  • This is a really great way to do what I wanted to achieve. Just figured out what my problem was: It was a Placeholer plugin which was clearing out the values from the textboxes. – Piyush Feb 05 '12 at 13:11
1

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";
coder
  • 13,002
  • 31
  • 112
  • 214
  • This sets the value attribute of the input control but does not change the display text. Have tried going this way. – Piyush Feb 05 '12 at 09:33
  • anyways @DonNetter I came across this code while I was searching for the solution before asking here, had tried implementing but to no avail. Thanks for putting in efforts buddy, really appreciate your help here. Let me know if you have anything more for me to try. I would be very happy to get one great solution. – Piyush Feb 05 '12 at 09:47
  • I have converted for you but I am not a C# guy sorry for if it has any mistakes. – coder Feb 05 '12 at 09:47
  • @Piyush-Thanks for your comment and sure If I come across this solution I will place here which might help you. – coder Feb 05 '12 at 09:48
0

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!

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • 1
    I'm not saying it's a bad idea to give the type explicitly, but I'm not sure this will help. In the absence of a type, `text` is the default, so this doesn't make a difference. – Mr Lister Feb 05 '12 at 09:24
  • @MrLister Totally true. This doesn't help. – Piyush Feb 05 '12 at 09:26