By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
SelectedIndexChanged 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 dropdownlistboxes 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 SelectedIndexChanged 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.SelectedIndexChanged, AddressOf _
Me.dlstWindow_SelectedIndexChanged

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_SelectedIndexChanged(ByVal sender As Object, ByVal _
e As System.EventArgs)

Dim strCurrentID As String
Dim currentDropDownList As DropDownList = CType(sender, DropDownList)

strCurrentID = currentDropDownList.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.

Feb 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

"Steve Moreno" <St*********@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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
SelectedIndexChanged 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 dropdownlistboxes 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 SelectedIndexChanged 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.SelectedIndexChanged, AddressOf _
Me.dlstWindow_SelectedIndexChanged

It looks like the event either doesn't fire or isn't in the correct
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

AddressOf. This is the sub I've been playing with:

Private Sub dlstWindow_SelectedIndexChanged(ByVal sender As Object, ByVal
_
e As System.EventArgs)

Dim strCurrentID As String
Dim currentDropDownList As DropDownList = CType(sender,
DropDownList)

strCurrentID = currentDropDownList.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.

Feb 6 '06 #2

P: n/a
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.

exact

:-)

Cor
Feb 7 '06 #3

P: n/a
Thanks for trying to help. I am already generating these controls on the
Page_Load event. I'm thinking the SelectedIndexChanged 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:
Hi,

"Steve Moreno" <St*********@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com...
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
SelectedIndexChanged 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 dropdownlistboxes 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 SelectedIndexChanged 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.SelectedIndexChanged, AddressOf _
Me.dlstWindow_SelectedIndexChanged

It looks like the event either doesn't fire or isn't in the correct


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

AddressOf. This is the sub I've been playing with:

Private Sub dlstWindow_SelectedIndexChanged(ByVal sender As Object, ByVal
_
e As System.EventArgs)

Dim strCurrentID As String
Dim currentDropDownList As DropDownList = CType(sender,
DropDownList)

strCurrentID = currentDropDownList.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.


Feb 7 '06 #4

P: n/a
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:
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
SelectedIndexChanged 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 dropdownlistboxes 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 SelectedIndexChanged 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.SelectedIndexChanged, AddressOf _
Me.dlstWindow_SelectedIndexChanged

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_SelectedIndexChanged(ByVal sender As Object, ByVal _
e As System.EventArgs)

Dim strCurrentID As String
Dim currentDropDownList As DropDownList = CType(sender, DropDownList)

strCurrentID = currentDropDownList.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.

Feb 7 '06 #5

P: 1
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.SelectedIndexChanged, AddressOf updateScore
quest_id.AutoPostBack = True

The event now fires!
Feb 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.