473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Wiring up event to a dynamically generated control

Hi,

I can't seem to find much information on this. Please could someone explain
to me how to wire up events (selectedIndexChanged) to a bunch of dynamically
created controls (Dropdownlists), preferably in C#. I'm sure this should be
relatively straighforward, but can't for the life of me figure out a good
way of doing it.

Many thanks!

S
Nov 20 '05 #1
5 1950
Private Sub Tester()

Dim list As New DropDownList

list.Items.Add("one")

list.Items.Add("two")

list.Items.Add("three")

list.Items.Add("four")

AddHandler list.SelectedIndexChanged, AddressOf listClick

End Sub

Protected Sub listClick(ByVal sender As Object, ByVal e As EventArgs)

'add your code here

End Sub

"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexChanged) to a bunch of
dynamically created controls (Dropdownlists), preferably in C#. I'm sure
this should be relatively straighforward, but can't for the life of me
figure out a good way of doing it.

Many thanks!

S

Nov 20 '05 #2
Yeah thats what I'm doing at the moment (well at least the c# equivalent).
However the event handler is not being called. Heres a sumary, phDropDowns
is a placeholder, DDL SelectedIndexChanged never gets called. I have a
feeling this is something to do with the Page.IsPostBack, but how else would
you do it?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

DropDownList tempDDL = new DropDownList();

tempDDL.SelectedIndexChanged += new
System.EventHandler(DDL_SelectedIndexChanged);

tempDDL.AutoPostBack = true;

phDropDowns.Controls.Add(tempDDL);

}
}
"Dimitrios Toulakis" <ma*********@web.de> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Private Sub Tester()

Dim list As New DropDownList

list.Items.Add("one")

list.Items.Add("two")

list.Items.Add("three")

list.Items.Add("four")

AddHandler list.SelectedIndexChanged, AddressOf listClick

End Sub

Protected Sub listClick(ByVal sender As Object, ByVal e As EventArgs)

'add your code here

End Sub

"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexChanged) to a bunch of
dynamically created controls (Dropdownlists), preferably in C#. I'm sure
this should be relatively straighforward, but can't for the life of me
figure out a good way of doing it.

Many thanks!

S


Nov 20 '05 #3
Sosh,

set the autopostback property of the dropdown to true

Dimi
"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Yeah thats what I'm doing at the moment (well at least the c# equivalent).
However the event handler is not being called. Heres a sumary,
phDropDowns is a placeholder, DDL SelectedIndexChanged never gets called.
I have a feeling this is something to do with the Page.IsPostBack, but how
else would you do it?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

DropDownList tempDDL = new DropDownList();

tempDDL.SelectedIndexChanged += new
System.EventHandler(DDL_SelectedIndexChanged);

tempDDL.AutoPostBack = true;

phDropDowns.Controls.Add(tempDDL);

}
}
"Dimitrios Toulakis" <ma*********@web.de> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Private Sub Tester()

Dim list As New DropDownList

list.Items.Add("one")

list.Items.Add("two")

list.Items.Add("three")

list.Items.Add("four")

AddHandler list.SelectedIndexChanged, AddressOf listClick

End Sub

Protected Sub listClick(ByVal sender As Object, ByVal e As EventArgs)

'add your code here

End Sub

"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexChanged) to a bunch of
dynamically created controls (Dropdownlists), preferably in C#. I'm
sure this should be relatively straighforward, but can't for the life of
me figure out a good way of doing it.

Many thanks!

S



Nov 20 '05 #4
Hi Dimi,

I'm already setting it to true (see below) - the page is being posted back,
but the event handler is not called

Thanks

"Dimitrios Toulakis" <ma*********@web.de> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Sosh,

set the autopostback property of the dropdown to true

Dimi
"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Yeah thats what I'm doing at the moment (well at least the c#
equivalent). However the event handler is not being called. Heres a
sumary, phDropDowns is a placeholder, DDL SelectedIndexChanged never
gets called. I have a feeling this is something to do with the
Page.IsPostBack, but how else would you do it?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

DropDownList tempDDL = new DropDownList();

tempDDL.SelectedIndexChanged += new
System.EventHandler(DDL_SelectedIndexChanged);

tempDDL.AutoPostBack = true;

phDropDowns.Controls.Add(tempDDL);

}
}
"Dimitrios Toulakis" <ma*********@web.de> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Private Sub Tester()

Dim list As New DropDownList

list.Items.Add("one")

list.Items.Add("two")

list.Items.Add("three")

list.Items.Add("four")

AddHandler list.SelectedIndexChanged, AddressOf listClick

End Sub

Protected Sub listClick(ByVal sender As Object, ByVal e As EventArgs)

'add your code here

End Sub

"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexChanged) to a bunch
of dynamically created controls (Dropdownlists), preferably in C#. I'm
sure this should be relatively straighforward, but can't for the life
of me figure out a good way of doing it.

Many thanks!

S



Nov 20 '05 #5
Hi Sosh,

Your problem is due to the fact that you need to rebuild all the controls
you have dynamically created on each post back. Every time the page is posted
back all the dynamically created controls are lost. So you need to recreate
them.
This should be done on or before page_load. So create a function to create
your dynamic controls and call this function on every postback. Then the
control is recreated and its event can be fired.

Kind Regards
Warren

"Sosh" wrote:
Hi Dimi,

I'm already setting it to true (see below) - the page is being posted back,
but the event handler is not called

Thanks

"Dimitrios Toulakis" <ma*********@web.de> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Sosh,

set the autopostback property of the dropdown to true

Dimi
"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Yeah thats what I'm doing at the moment (well at least the c#
equivalent). However the event handler is not being called. Heres a
sumary, phDropDowns is a placeholder, DDL SelectedIndexChanged never
gets called. I have a feeling this is something to do with the
Page.IsPostBack, but how else would you do it?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

DropDownList tempDDL = new DropDownList();

tempDDL.SelectedIndexChanged += new
System.EventHandler(DDL_SelectedIndexChanged);

tempDDL.AutoPostBack = true;

phDropDowns.Controls.Add(tempDDL);

}
}
"Dimitrios Toulakis" <ma*********@web.de> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Private Sub Tester()

Dim list As New DropDownList

list.Items.Add("one")

list.Items.Add("two")

list.Items.Add("three")

list.Items.Add("four")

AddHandler list.SelectedIndexChanged, AddressOf listClick

End Sub

Protected Sub listClick(ByVal sender As Object, ByVal e As EventArgs)

'add your code here

End Sub

"Sosh" <so*******@something.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> I can't seem to find much information on this. Please could someone
> explain to me how to wire up events (selectedIndexChanged) to a bunch
> of dynamically created controls (Dropdownlists), preferably in C#. I'm
> sure this should be relatively straighforward, but can't for the life
> of me figure out a good way of doing it.
>
> Many thanks!
>
> S
>



Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Oisin Grehan | last post by:
Hi, I have a UserControl derived class: <ns:votingbutton runat="server" id="btn1" onclick="votingbuttonclick" /> My question is, what code do I need in place in the codebehind for this to...
0
by: luca | last post by:
Hi all. My problem is that I can't handle events raised from child components within a composite server control when the control is created dynamically. Everything works fine if the same control...
0
by: Homam | last post by:
To wire a Click event handler to a dynamically-added control (e.g. LinkButton), you need to do so during the Load event of the parent (e.g. a PlaceHolder). The problem is, the Click event...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
4
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime,...
1
by: bill | last post by:
I'm using VS2005. I am dynamically adding a Textbox control to a Placeholder control in the page_load event of the form, but the event handler isn't firing. What am I doing wrong? Thanks...
5
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
18
by: Redhairs | last post by:
Is it possible to get DropDownList.SelectedValue in Page_PreInit() event during the postback?
0
by: CMELLO | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.