TextBox.Text value not changing when it is prepopulated

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default TextBox.Text value not changing when it is prepopulated

    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.
    [email]paul_m_frazier@wideopenwest.com[/email]

    Eschew Obfuscation!


    Paul M. Frazier, Ph.D. Guest

  2. Similar Questions and Discussions

    1. Changing text without changing frames.
      I have a small flash movie that is basically a color chooser. Rather than changing the color of an item by putting the color in a frame I change...
    2. Changing text to a textbox control in row 5 of a datagrid
      dim txt as textbox txt=datagrid1.item(0).controls(2) txt.text=xxxxxx "William Pulling via .NET 247" <anonymous@dotnet247.com>, haber iletisinde...
    3. changing the size of a textbox in a datagrid?
      Hi There, I would like to find out if I can modify some of the properties of a textbox within a datagrid? 1. How can modify the width of the...
    4. Selecting all text in a textbox?
      I've got a server-side textbox that receives the focus when the page loads. It has the text "Refine your Search" in the textbox. Obviously the user...
    5. formatting text in textbox while typing
      Hi .. did you accomplish it? If yes .. could you please inform me what should I do? I met the same problem .. many thanks in advance Best...
  3. #2

    Default Re: TextBox.Text value not changing when it is prepopulated


    "Paul M. Frazier, Ph.D." <paul_m_frazier@wideopenwest.com> wrote in message
    news:Bn-dnTFM0MgnYrmiXTWJjQ@wideopenwest.com...
    > 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


    Luc Kumps Guest

  4. #3

    Lightbulb Re: TextBox.Text value not changing when it is prepopulated

    THANK YOU!
    That one almost drove me mad, and it was that "simple". The postback replaced the entries done updating the database with the old values.
    Unregistered Guest

  5. #4

    Default Re: TextBox.Text value not changing when it is prepopulated

    Quote Originally Posted by Luc Kumps View Post
    "Paul M. Frazier, Ph.D." <paul_m_frazier@wideopenwest.com> wrote in message
    news:Bn-dnTFM0MgnYrmiXTWJjQ@wideopenwest.com...

    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
    Excellent suggestion it's working thank you for your advice
    -Jayadev Gyani
    Jayadev Gyani Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139