[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2004-10-28
16
Medium Priority
?
713 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

611 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