Results 1 to 3 of 3

Thread: Strange reaction in dynamically created textbox and addhandler

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2023
    Posts
    2

    Strange reaction in dynamically created textbox and addhandler

    Hi,

    Just moved back to vb.net after years of C#. Due to some unknowns in the amount of a certain entity (i.e. a variable number of buildings each having it's own screen) I decided to write the code entirely dynamically. All variables are "Dimed"and there is no declarative code in the section I am writing. I have been successful up to a point. However I Dimed a text box and set up a click event using addhandler. The weird thing that happens is that when I type some numbers and click return in txtStartDate it does not go to the Private Sub below. What is even weirder is that it does go to an image button click event in the Site.Master page that is connected to the page where the rest of the code is. So despite the fact that the image button is not in the page where the code below is, it acts like the image button in Site.Master has been clicked when it hasn't. It's almost like the txtStartDate.TextChanged that occurs when i click enter in the textbox is somehow creating a click of the image button in Site.master.

    Very confused. Any help would be appreciated.

    Code:
    Dim txtStartDate = New TextBox
    txtStartDate.ID = "StartDate"
    txtStartDate.AutoPostBack = True
    
    AddHandler txtStartDate.TextChanged, AddressOf txtStartDate_TextChanged
    
    Private Sub txtStartDate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If CType(sender, Date) < CType("May 6, 2014", Date) Then
            Dim a As Integer
            a = 3
        End If
    End Sub
    Here is the imagebutton click event in the site.master file.

    Code:
    Protected Sub ibHome_Click(sender As Object, e As ImageClickEventArgs) Handles ibHome.Click
        Response.Redirect("~/Default.aspx")
    End Sub
    Last edited by dday9; Dec 19th, 2023 at 05:33 PM. Reason: Added code tags

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,212

    Re: Strange reaction in dynamically created textbox and addhandler

    Quote Originally Posted by fig0000 View Post
    However I Dimed a text box and set up a click event using addhandler. The weird thing that happens is that when I type some numbers and click return in txtStartDate it does not go to the Private Sub below. What is even weirder is that it does go to an image button click event in the Site.Master page that is connected to the page where the rest of the code is.
    Please clarify what you mean by "click return in txtStartDate". Do you mean press the Enter key? If so, then what is likely happening is the button that is treated as the "Default Button" is having it's click event raised because that's how the UI works by default when the Enter key is pressed while in a TextBox.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2023
    Posts
    2

    Re: Strange reaction in dynamically created textbox and addhandler

    Thanks for answering so quickly. I apologize for my unclear terminology. Yes I meant that I pressed the enter key in txtStartDate. As far as the default button you mention I won't argue since there may be something I don't know. However the event set off hitting enter in txtStartDate is not related to a key. That event is for an image button click in the Master page. So the enter is setting off an event for an image button that is in a different page (The master page) than the one the code is in. Weird, I Know but I have a hard time believing that pressing enter in a text box in the child page of amaster page would set of an image button click event IN the masterpage. Keep in mind that this same master page event is not being set off by a click of the image button or being called in code. Nothing happening to set this even off. If there is some weird rule that I don't understand I will ask that you clarify. I will also point out that there is other code that is similar to the code I've shown in this thread that does no have this affect.

Posting Permissions

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



Click Here to Expand Forum to Full Width