Simplest possible question?

  • Thread starter Thread starter qqq
  • Start date Start date
Q

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
 
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 said:
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
 
Back
Top