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

G

Guest

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.
 
B

Bart Mermuys

Hi,

Steve Moreno said:
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
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top