Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-28
16
Medium Priority
?
693 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
Comment
Question by:dspace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
16 Comments
 
LVL 2

Expert Comment

by:ChillAmmar
ID: 12432800
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
ID: 12432845
LostFocus doesn't appear to be a method for a web vb.net app.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12432993
You can either create button to display a changed text or use JavaScript blur event
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dspace
ID: 12433076
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
ID: 12433100
Is the EnableViewState Property enabled on your control ?
0
 

Author Comment

by:dspace
ID: 12433136
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
ID: 12433148
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
ID: 12433281
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
ID: 12433343
--> 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
ID: 12433349
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
ID: 12433538
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
ID: 12433590
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
ID: 12433715
Sure, what is the best way to do that?
0
 
LVL 8

Expert Comment

by:toddhd
ID: 12433822
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:
toddhd earned 2000 total points
ID: 12434016
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
ID: 12434083
Thank you, thank you Thank you!!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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