2nd Post: Problem adding events to controls created at run-time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Moreno

    #1

    2nd Post: Problem adding events to controls created at run-time

    Hi all,

    I've got a web form that I've written code to create an array of
    DropDownList controls on the page depending on how many records are pulled
    back. The code to create the controls is working fine but now I need to add
    events to the newly created DropDownList controls. I need to add the
    SelectedIndexCh anged event and I'm having a hard time getting the code to add
    events to the controls since the names (IDs) are varied to include the record
    row number like "name & (int)".

    I've seen examples on other web-sites which have either syntax errors and/or
    so do not compile on my pc. So my question is this...

    If I have a group of dropdownlistbox es named dlstDropDown and I have changed
    the ID of the controls before I add the control to my page to include an
    index so they would be dlstDropDown0, dlstDropDown1, dlstDropDown2, etc...
    How do I add a SelectedIndexCh anged event to each of them? And/Or how would
    I go about creating a generic private sub to capture this event?

    What I've tried so far with no success is:

    'before I add the control to the page
    AddHandler dlstWindow.Sele ctedIndexChange d, AddressOf _
    Me.dlstWindow_S electedIndexCha nged

    It looks like the event either doesn't fire or isn't in the correct
    AddressOf. This is the sub I've been playing with:

    Private Sub dlstWindow_Sele ctedIndexChange d(ByVal sender As Object, ByVal _
    e As System.EventArg s)

    Dim strCurrentID As String
    Dim currentDropDown List As DropDownList = CType(sender, DropDownList)

    strCurrentID = currentDropDown List.ID

    End Sub

    I'm using 2003 (haven't migrated to 2005 yet). Any help would be greatly
    appreciated. I've already burned an entire week on this and am working on
    burning another one. :(

    thanks in advance,
    Steve.

  • Bart Mermuys

    #2
    Re: 2nd Post: Problem adding events to controls created at run-time

    Hi,

    "Steve Moreno" <SteveMoreno@di scussions.micro soft.com> wrote in message
    news:A98C5DF6-484A-4B4A-9671-04B5DE6D7D86@mi crosoft.com...[color=blue]
    > Hi all,
    >
    > I've got a web form that I've written code to create an array of
    > DropDownList controls on the page depending on how many records are pulled
    > back. The code to create the controls is working fine but now I need to
    > add
    > events to the newly created DropDownList controls. I need to add the
    > SelectedIndexCh anged event and I'm having a hard time getting the code to
    > add
    > events to the controls since the names (IDs) are varied to include the
    > record
    > row number like "name & (int)".
    >
    > I've seen examples on other web-sites which have either syntax errors
    > and/or
    > so do not compile on my pc. So my question is this...
    >
    > If I have a group of dropdownlistbox es named dlstDropDown and I have
    > changed
    > the ID of the controls before I add the control to my page to include an
    > index so they would be dlstDropDown0, dlstDropDown1, dlstDropDown2, etc...
    > How do I add a SelectedIndexCh anged event to each of them? And/Or how
    > would
    > I go about creating a generic private sub to capture this event?
    >
    > What I've tried so far with no success is:
    >
    > 'before I add the control to the page
    > AddHandler dlstWindow.Sele ctedIndexChange d, AddressOf _
    > Me.dlstWindow_S electedIndexCha nged
    >
    > It looks like the event either doesn't fire or isn't in the correct[/color]

    I rarely make webapplications , but from what i remember you need to
    re-create the dynamic Controls in Page_Load with the same ID. WebPage's are
    static, so when the page is loaded again (because of postback) the dynamic
    controls are gone so their events can't fire. But if you re-create them
    inside Page_Load with the same id, then their events will fire.

    HTH,
    Greetings

    [color=blue]
    > AddressOf. This is the sub I've been playing with:
    >
    > Private Sub dlstWindow_Sele ctedIndexChange d(ByVal sender As Object, ByVal
    > _
    > e As System.EventArg s)
    >
    > Dim strCurrentID As String
    > Dim currentDropDown List As DropDownList = CType(sender,
    > DropDownList)
    >
    > strCurrentID = currentDropDown List.ID
    >
    > End Sub
    >
    > I'm using 2003 (haven't migrated to 2005 yet). Any help would be greatly
    > appreciated. I've already burned an entire week on this and am working on
    > burning another one. :(
    >
    > thanks in advance,
    > Steve.
    >[/color]


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: 2nd Post: Problem adding events to controls created at run-time

      [color=blue]
      > I rarely make webapplications , but from what i remember you need to
      > re-create the dynamic Controls in Page_Load with the same ID. WebPage's
      > are static, so when the page is loaded again (because of postback) the
      > dynamic controls are gone so their events can't fire. But if you
      > re-create them inside Page_Load with the same id, then their events will
      > fire.
      >[/color]
      exact

      :-)

      Cor


      Comment

      • Steve Moreno

        #4
        Re: 2nd Post: Problem adding events to controls created at run-tim

        Thanks for trying to help. I am already generating these controls on the
        Page_Load event. I'm thinking the SelectedIndexCh anged events are firing but
        maybe my syntax for the handler sub is incorrect? Please check below and see
        if you can find an issue why this doesn't work.

        Thanks In Advance Again.
        Steve.


        "Bart Mermuys" wrote:
        [color=blue]
        > Hi,
        >
        > "Steve Moreno" <SteveMoreno@di scussions.micro soft.com> wrote in message
        > news:A98C5DF6-484A-4B4A-9671-04B5DE6D7D86@mi crosoft.com...[color=green]
        > > Hi all,
        > >
        > > I've got a web form that I've written code to create an array of
        > > DropDownList controls on the page depending on how many records are pulled
        > > back. The code to create the controls is working fine but now I need to
        > > add
        > > events to the newly created DropDownList controls. I need to add the
        > > SelectedIndexCh anged event and I'm having a hard time getting the code to
        > > add
        > > events to the controls since the names (IDs) are varied to include the
        > > record
        > > row number like "name & (int)".
        > >
        > > I've seen examples on other web-sites which have either syntax errors
        > > and/or
        > > so do not compile on my pc. So my question is this...
        > >
        > > If I have a group of dropdownlistbox es named dlstDropDown and I have
        > > changed
        > > the ID of the controls before I add the control to my page to include an
        > > index so they would be dlstDropDown0, dlstDropDown1, dlstDropDown2, etc...
        > > How do I add a SelectedIndexCh anged event to each of them? And/Or how
        > > would
        > > I go about creating a generic private sub to capture this event?
        > >
        > > What I've tried so far with no success is:
        > >
        > > 'before I add the control to the page
        > > AddHandler dlstWindow.Sele ctedIndexChange d, AddressOf _
        > > Me.dlstWindow_S electedIndexCha nged
        > >
        > > It looks like the event either doesn't fire or isn't in the correct[/color]
        >
        > I rarely make webapplications , but from what i remember you need to
        > re-create the dynamic Controls in Page_Load with the same ID. WebPage's are
        > static, so when the page is loaded again (because of postback) the dynamic
        > controls are gone so their events can't fire. But if you re-create them
        > inside Page_Load with the same id, then their events will fire.
        >
        > HTH,
        > Greetings
        >
        >[color=green]
        > > AddressOf. This is the sub I've been playing with:
        > >
        > > Private Sub dlstWindow_Sele ctedIndexChange d(ByVal sender As Object, ByVal
        > > _
        > > e As System.EventArg s)
        > >
        > > Dim strCurrentID As String
        > > Dim currentDropDown List As DropDownList = CType(sender,
        > > DropDownList)
        > >
        > > strCurrentID = currentDropDown List.ID
        > >
        > > End Sub
        > >
        > > I'm using 2003 (haven't migrated to 2005 yet). Any help would be greatly
        > > appreciated. I've already burned an entire week on this and am working on
        > > burning another one. :(
        > >
        > > thanks in advance,
        > > Steve.
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Steve Moreno

          #5
          RE: 2nd Post: Problem adding events to controls created at run-time

          Got it!!!!!!!!!!!!! !

          With the replies I've received from Ken, Bart, and Cor I've realized that I
          had the code that creates the controls within a "If Not IsPostBack Then"
          block and so the controls were not being re-created and thus events were
          indeed not firing. Something so obvious and yet not obvious until you hear
          from others... Now that I've modified the code to reside outside of the "If
          Not IsPostBack Then" block, it works GREAT!

          Thanks to all who have replied.
          Steve.


          "Steve Moreno" wrote:
          [color=blue]
          > Hi all,
          >
          > I've got a web form that I've written code to create an array of
          > DropDownList controls on the page depending on how many records are pulled
          > back. The code to create the controls is working fine but now I need to add
          > events to the newly created DropDownList controls. I need to add the
          > SelectedIndexCh anged event and I'm having a hard time getting the code to add
          > events to the controls since the names (IDs) are varied to include the record
          > row number like "name & (int)".
          >
          > I've seen examples on other web-sites which have either syntax errors and/or
          > so do not compile on my pc. So my question is this...
          >
          > If I have a group of dropdownlistbox es named dlstDropDown and I have changed
          > the ID of the controls before I add the control to my page to include an
          > index so they would be dlstDropDown0, dlstDropDown1, dlstDropDown2, etc...
          > How do I add a SelectedIndexCh anged event to each of them? And/Or how would
          > I go about creating a generic private sub to capture this event?
          >
          > What I've tried so far with no success is:
          >
          > 'before I add the control to the page
          > AddHandler dlstWindow.Sele ctedIndexChange d, AddressOf _
          > Me.dlstWindow_S electedIndexCha nged
          >
          > It looks like the event either doesn't fire or isn't in the correct
          > AddressOf. This is the sub I've been playing with:
          >
          > Private Sub dlstWindow_Sele ctedIndexChange d(ByVal sender As Object, ByVal _
          > e As System.EventArg s)
          >
          > Dim strCurrentID As String
          > Dim currentDropDown List As DropDownList = CType(sender, DropDownList)
          >
          > strCurrentID = currentDropDown List.ID
          >
          > End Sub
          >
          > I'm using 2003 (haven't migrated to 2005 yet). Any help would be greatly
          > appreciated. I've already burned an entire week on this and am working on
          > burning another one. :(
          >
          > thanks in advance,
          > Steve.
          >[/color]

          Comment

          • smg1965
            New Member
            • Feb 2006
            • 1

            #6
            Had same problem, hence the reason for finding this thread.

            Solved by adding this last line to similar code:

            quest_id = New DropDownList
            AddHandler quest_id.Select edIndexChanged, AddressOf updateScore
            quest_id.AutoPo stBack = True

            The event now fires!

            Comment

            Working...