Solved

Textbox Text Property remains unchanged even though it fires the textchanged method.

Posted on 2004-10-28
644 Views
Last Modified: 2012-06-27
I have a vb.net web app with the following code:

Private Sub txtEndDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEndDate.TextChanged
        Dim dateNewDate As Date
        dateNewDate = txtEndDate.Text
        Response.Write("<br>" & dateNewDate.ToShortDateString)
End Sub

Even though I enter a new date on the screen, the response.write shows the original date.

Why doesn't this work?????????
0
Question by:dspace
    16 Comments
     
    LVL 2

    Expert Comment

    by:ChillAmmar
    Hi,
      the response.Write method displays the original date since it get fired every time you key in something (whether you have entered a valid date or not).
    You can use the lost_focus method of the text box.
    And write the same code as in the TextChanged event under LostFocus event
    0
     

    Author Comment

    by:dspace
    LostFocus doesn't appear to be a method for a web vb.net app.
    0
     
    LVL 28

    Expert Comment

    by:iboutchkine
    You can either create button to display a changed text or use JavaScript blur event
    0
     

    Author Comment

    by:dspace
    Sorry, I guess I should have included all the code.  I didn't think the response.write would create a problem in its own right.

    The actual code is:

    Private Sub txtEndDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEndDate.TextChanged
            Dim dateNewDate As Date
            dateNewDate = txtEndDate.Text
            If (dateNewDate >= DateSerial(2003, 12, 22)) And (dateNewDate <= Session("asp_CurrentPayPeriodEnd")) Then
                ChangeDate(dateNewDate)
            End If
    End Sub

    When this code was executed the date wasn't changing.  The change date subroutine retrieves data from a sql server table and displays info on a form.  The info reflected the original date so I just stuck the response.write in to verify.

    The strange thing is that this same code is in two other forms and works fine.
    0
     
    LVL 4

    Expert Comment

    by:Glom
    Is the EnableViewState Property enabled on your control ?
    0
     

    Author Comment

    by:dspace
    Yes, along with enabled, autopostback,visible.

    For lack of any other idea, I tried changing autopostback to false but that didn't help.
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    It works for me. Here is my sample code, compare yours to mine, see if something is different. Note that turned on AutoPostback for the textbox, but you could turn that off and use a submit button (I did that first, it still worked).

    Public Class WebForm1
        Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents txtEndDate As System.Web.UI.WebControls.TextBox

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

    #End Region

        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
        End Sub

     
        Private Sub txtEndDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEndDate.TextChanged
            Dim dateNewDate As Date
            dateNewDate = txtEndDate.Text
            Response.Write("<br>" & dateNewDate.ToShortDateString)
        End Sub
    End Class

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="TestWeb.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
          <HEAD>
                <title>WebForm1</title>
                <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
                <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
                <meta name="vs_defaultClientScript" content="JavaScript">
                <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
          </HEAD>
          <body MS_POSITIONING="GridLayout">
                <form id="Form1" method="post" runat="server">
                      <asp:TextBox id="txtEndDate" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
                            AutoPostBack="True">11/12/04</asp:TextBox>
                </form>
          </body>
    </HTML>
    0
     

    Author Comment

    by:dspace
    Sadly, my code looks the same.  As I said, I have this same code in two other forms in the same project and it works fine.

    I really appreciate all your time, thanks.
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    --> Even though I enter a new date on the screen, the response.write shows the original date.

    When are you initializing your Text box with Date?

    make sure you're initializing it when IsPostBack is False, otherwise you'll alwayz get original value back, even if you change it.

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
     If Not IsPostBack Then
           txtEndDate.Text = DateTime.Now.ToShortDateString()
     End If
    End Sub

    -tushar
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    Don't give up hope. Something is messing with the order that the events are firing, or the value of the textbox.

    Is your page cacheing at all?

    Do you have the viewstate disabled?

    If you are using autopostback, turn it off and try using a submit button - or vice-versa - see if the result changes.


    0
     

    Author Comment

    by:dspace
    I'm initializing it on Page Load and IsPostBack is False.

    Since it works on two other forms in the project, it has to be something related to this specific form.  I have checked the Web Form Designer Generated Code against a form that works and the generated code is identical (except for the list of controls).

    I can't have the control as a submit button because I want the user to be able to edit the displayed date.

    Viewstate is enabled.

    If page caching were a problem wouldn't it be a problem with all the forms in the project?
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    Any chance you could email me the the aspx and vb for this page - a second pair of eyes might help?
    0
     

    Author Comment

    by:dspace
    Sure, what is the best way to do that?
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    zip it up and send it to my gmail.com account - my gmail name is the same as my username here - I won't post it to avoid the spam bots.
    0
     
    LVL 8

    Accepted Solution

    by:
    Thanks, got it - the problem is here:

        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
            lblApprovalTitle.Text = "Timesheet Approvals for the Pay Period Ending "
            txtEndDate.Text = Session("asp_PayPeriodEnd")

    Page_Load is firing on every submit. So the user changes the date, presses enter, and txtEndDate is reloaded with the value from the session. This occurs BEFORE the txtchanged event, so you just keep getting the same value. So just do 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

            lblApprovalTitle.Text = "Timesheet Approvals for the Pay Period Ending "
            txtEndDate.Text = Session("asp_PayPeriodEnd")
    0
     

    Author Comment

    by:dspace
    Thank you, thank you Thank you!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now