By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,571 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,571 IT Pros & Developers. It's quick & easy.

Simplest possible question?

P: n/a
qqq
Ok, what's wrong with this picture? I have a simple webform with a single
textbox webcontrol and a single webcontrol button. When the form is
displayed, I manually type some new text into the textbox. Why aren't my
updates detected in the button's code?!

[It makes no difference how I set AutoPostBack or EnableViewState]

Thanks, Paul.
-------
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 = "Init value goes in here"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextBox2.Text = TextBox1.Text '<<This always detects ""Init value goes in
here" even if I've edited it on-screen!!

End Sub
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Change Page_Load to this:

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 = "Init value goes in here"
End If

End Sub

A simple question, gets a simple answer. :)

Greg
"qqq" <qq*@qqq.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
Ok, what's wrong with this picture? I have a simple webform with a single
textbox webcontrol and a single webcontrol button. When the form is
displayed, I manually type some new text into the textbox. Why aren't my
updates detected in the button's code?!

[It makes no difference how I set AutoPostBack or EnableViewState]

Thanks, Paul.
-------
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 = "Init value goes in here"

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextBox2.Text = TextBox1.Text '<<This always detects ""Init value goes in
here" even if I've edited it on-screen!!

End Sub

Nov 20 '05 #2

P: n/a
"qqq" <qq*@qqq.com> wrote in message news:ee**************@tk2msftngp13.phx.gbl...
Ok, what's wrong with this picture? I have a simple webform with a single
textbox webcontrol and a single webcontrol button. When the form is
displayed, I manually type some new text into the textbox. Why aren't my
updates detected in the button's code?!


Because on every Page_Load you are initializing TextBox1.Text to "Init value goes in here",
therefore whatever you typed (which ASP.NET put into TextBox1.Text prior to Page_Load)
is overwritten by the assignment. After Page_Load, the Button1_Click event fires, and it
will always see "Init value goes in here."

What you probably want to do in Page_Load is only initialize the TextBox1.Text on the first
request to the page (and not re-initialize it on subsequent requests, because the viewstate of
the TextBox will retain it's value).

If ( Not Page.IsPostBack ) Then
TextBox1.Text = "Init value goes in here"
End If
Derek Harmon
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.