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

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?????????
dspaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChillAmmarCommented:
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
dspaceAuthor Commented:
LostFocus doesn't appear to be a method for a web vb.net app.
0
iboutchkineCommented:
You can either create button to display a changed text or use JavaScript blur event
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

dspaceAuthor Commented:
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
GlomCommented:
Is the EnableViewState Property enabled on your control ?
0
dspaceAuthor Commented:
Yes, along with enabled, autopostback,visible.

For lack of any other idea, I tried changing autopostback to false but that didn't help.
0
toddhdCommented:
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
dspaceAuthor Commented:
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
tusharashahCommented:
--> 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
toddhdCommented:
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
dspaceAuthor Commented:
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
toddhdCommented:
Any chance you could email me the the aspx and vb for this page - a second pair of eyes might help?
0
dspaceAuthor Commented:
Sure, what is the best way to do that?
0
toddhdCommented:
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
toddhdCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dspaceAuthor Commented:
Thank you, thank you Thank you!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.