I would like that each the button is pressed, the textbox shows the time.
How to achieve this? Now the page is refreshed and the code is called, but the value of the textbox does not change.
NOTE: I of course don’t need completely new code achieving just that. I need to be able to do it in the code behind file of an asp file.
protected void Page_Load(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "time";
Button submit = new Button();
submit.Text = "Update";
submit.UseSubmitBehavior = true;
form1.Controls.Add(t);
form1.Controls.Add(submit);
t.Text = DateTime.Now.ToString();
}
Update:
Try this approach, creating the child controls in the wrong place leads to weird effects like you see above, because the event order is all off, e.g. the page’s load is firing before the control’s Init. The CreateChildControls happens in the page’s PreRender event, you can see here for a full breakdown. For a reference of when ViewState’s loaded: this is a better view.
Since ASP.Net only uses one form by default, page has a Form property you can use.