Bug in TextBox control ?
-
Hi, I'm using the control TextBox: protected System.Web.UI.WebControls.TextBox TextBox1; In the Page_Load function I'm initializing some value to the TextBox: TextBox1.Text="initial data"; The problem is that once I'm initializing a string to the TextBox control the changes that the user makes in the text are not taking place. When I'm getting the data from the control, for instance: string s=TextBox1.Text; the string I get is exactly the same as the one which was initialized. What can solve that ? Thanks
-
Hi, I'm using the control TextBox: protected System.Web.UI.WebControls.TextBox TextBox1; In the Page_Load function I'm initializing some value to the TextBox: TextBox1.Text="initial data"; The problem is that once I'm initializing a string to the TextBox control the changes that the user makes in the text are not taking place. When I'm getting the data from the control, for instance: string s=TextBox1.Text; the string I get is exactly the same as the one which was initialized. What can solve that ? Thanks
Ah! I made this mistake, too, until I stumbled across the answer. Whenever you are making your changes, you most normally have some type of submit button, I'm sure. So... You are expecting that the data the user inputs is going to be in the ViewState, and will stay in the textbox that the user input data into after he/she submits. Well, a Page_Load happens _every_ time the webpage refreshes, not just the very first time the particular application loads. So instead of doing just: TextBox1.Text="initialdata"; You should check to see whether or not the page is being posted back to itself, or in other words, whether or not the user just clicked a button to load the page, instead of just surfing to the page. You can do so by doing this in your Page_Load handler: If(!isPostBack()) { TextBox1.Text="initialdata"; } This way, the page will only initialize the text property when isPostBack returns false (which would be when the visitor hit the page for the very first time). Hope that helps! --Keith