TextBox.Text value not changing when it is prepopulated

  • Thread starter Thread starter Paul M. Frazier, Ph.D.
  • Start date Start date
P

Paul M. Frazier, Ph.D.

I am writing a user information update page and I populate the form on
Page_Load with the current values of the user's name, etc. When I
change the text in one of the textbox controls (e.g., change the
user's middle initial from M. to X.) and submit the form, the value
seen for middleinitial.text in the Public Sub that is executed when
the Update button is clicked is the initial value (M.) and not the new
value (X.). Is there something I need to do to make the codebehind
file see the new value and not the one set up in Page_Load?

Here are code snippets from register.aspx and register.aspx.vb. Note
that I am using Visual Studio .NET as my development tool.

Code from register.aspx:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="register.aspx.vb" Inherits="dotNETDemo.register"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>register</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table border="0">
<tr>

<snip>

<tr>
<td>Middle Initial:
</td>
<td><asp:textbox id="MiddleInitial" runat="server"
MaxLength="2"></asp:textbox></td>
</tr>

<snip>

<tr>
<td align="middle" colSpan="2"><asp:button id="Register"
onclick="Register_Click" Runat="server"
Text="Register"></asp:button>&nbsp;&nbsp;
<asp:button id="Cancel" onclick="Cancel_Click" Runat="server"
Text="Cancel" CausesValidation="False"></asp:button></td>
</tr>
</table>
</form>
</body>
</HTML>


Code from register.aspx.vb:

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls


Public Class register
Inherits System.Web.UI.Page

<snip>

Protected WithEvents MiddleInitial As
System.Web.UI.WebControls.TextBox

<snip>

Protected WithEvents Register As System.Web.UI.WebControls.Button

<snip>


Public Sub Register_Click(ByVal sender As Object, ByVal e As
EventArgs)
' Put code to handle adding a new user here.
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim prm As SqlClient.SqlParameter
Dim DBError As String
Dim UserID As Integer
Dim ReturnCode As String
Dim PersonalizationCookie As New HttpCookie("UserID")

' At this point, a Response.Write(MiddleInitial.Text) gives the
original value of "M.",
even after the value in the textbox has been changed to "X."

cn = New SqlConnection("Data Source=(local);Initial
Catalog=ArachnidWebWorks;Integrated Security=SSPI;")
cmd = New SqlCommand("spInsertNewUser", cn)
cmd.CommandType = CommandType.StoredProcedure

<remaining code to execute the sproc, etc.>

Any advice is welcome here.

Thanks,
Paul


---

Paul M. Frazier, Ph.D.
(e-mail address removed)

Eschew Obfuscation!
 
Paul M. Frazier said:
I am writing a user information update page and I populate the form on
Page_Load with the current values of the user's name, etc. When I
change the text in one of the textbox controls (e.g., change the
user's middle initial from M. to X.) and submit the form, the value
seen for middleinitial.text in the Public Sub that is executed when
the Update button is clicked is the initial value (M.) and not the new
value (X.). Is there something I need to do to make the codebehind
file see the new value and not the one set up in Page_Load?

Page_Load is called *every* time: not only when displaying the page
originally, but also every time it's posted back (submitted).
You need to test if "IsPostBack" is true: in that case, you shouldn't
initialize your textbox...
Something like this will do:

if IsPostBack then
... initialize testbox
endif


Luc K
 
Back
Top