Webcontrols.Textbox Problem in retrieving text
Hi all, I'm new to ASP.NET, can you give me a hand?
I have a Web Form with a WebControls.Button and a WebControls.Textbox on it, and with the following code behind:
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
TextBox1.Text = "Enter Text Here"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Write("You have entered :" & TextBox1.Text)
End Sub
I would like to set the text on the textbox object in the Load Event, and it's up to user modification.
After all, it should display the modifided text upon clicking the button.
Sounds simple isn't it?
But to my surpeise, TextBox1.text remains the initial value (i.e "Enter Text Here"), and it ignores users' modification. Can somebody advise on it?
Re: Webcontrols.Textbox Problem in retrieving text
Did you save your changes before running it?
Re: Webcontrols.Textbox Problem in retrieving text
Quote:
Originally Posted by simonm
Did you save your changes before running it?
Do you mean saving the project/aspx, etc files, before rebuilding the solution? yes i did.
:(
Re: Webcontrols.Textbox Problem in retrieving text
I found something related to the problem:
If the Textbox control has NOT been assigned a value to its text property programtically (i.e textbox1.text ="something"), there won't be such a problem.
however, when TextboxBox1.text has been set by code, the problem raised
Re: Webcontrols.Textbox Problem in retrieving text
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
TextBox1.Text = "Enter Text Here"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Write("You have entered :" & TextBox1.Text)
End Sub
Re: Webcontrols.Textbox Problem in retrieving text
The problem is you are setting the value of the TextBox on Page_Load. When the button is clicked, it causes the form to be posted again and a new Page_Load which sets the text back to the original.
Re: Webcontrols.Textbox Problem in retrieving text
Oh, that works!
Thanks mendhak and wey97!
Re: Webcontrols.Textbox Problem in retrieving text
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
TextBox1.Text = "Enter Text Here"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Textbox1.text = Response.Write("You have entered :" & TextBox1.Text)
End Sub
I know this has already been solved but i thought i would post also that you could do it this way.....this should work
Re: Webcontrols.Textbox Problem in retrieving text