473,796 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 (selectedIndexC hanged) 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 1967
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.SelectedIn dexChanged, AddressOf listClick

End Sub

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

'add your code here

End Sub

"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexC hanged) 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 SelectedIndexCh anged 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(objec t sender, System.EventArg s e)

{

if(!Page.IsPost Back)

{

DropDownList tempDDL = new DropDownList();

tempDDL.Selecte dIndexChanged += new
System.EventHan dler(DDL_Select edIndexChanged) ;

tempDDL.AutoPos tBack = true;

phDropDowns.Con trols.Add(tempD DL);

}
}
"Dimitrios Toulakis" <ma*********@we b.de> wrote in message
news:u5******** ******@TK2MSFTN GP14.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.SelectedIn dexChanged, AddressOf listClick

End Sub

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

'add your code here

End Sub

"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexC hanged) 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*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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 SelectedIndexCh anged 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(objec t sender, System.EventArg s e)

{

if(!Page.IsPost Back)

{

DropDownList tempDDL = new DropDownList();

tempDDL.Selecte dIndexChanged += new
System.EventHan dler(DDL_Select edIndexChanged) ;

tempDDL.AutoPos tBack = true;

phDropDowns.Con trols.Add(tempD DL);

}
}
"Dimitrios Toulakis" <ma*********@we b.de> wrote in message
news:u5******** ******@TK2MSFTN GP14.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.SelectedIn dexChanged, AddressOf listClick

End Sub

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

'add your code here

End Sub

"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexC hanged) 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*********@we b.de> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Sosh,

set the autopostback property of the dropdown to true

Dimi
"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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 SelectedIndexCh anged 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(objec t sender, System.EventArg s e)

{

if(!Page.IsPost Back)

{

DropDownList tempDDL = new DropDownList();

tempDDL.Selecte dIndexChanged += new
System.EventHan dler(DDL_Select edIndexChanged) ;

tempDDL.AutoPos tBack = true;

phDropDowns.Con trols.Add(tempD DL);

}
}
"Dimitrios Toulakis" <ma*********@we b.de> wrote in message
news:u5******** ******@TK2MSFTN GP14.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.SelectedIn dexChanged, AddressOf listClick

End Sub

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

'add your code here

End Sub

"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I can't seem to find much information on this. Please could someone
explain to me how to wire up events (selectedIndexC hanged) 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*********@we b.de> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Sosh,

set the autopostback property of the dropdown to true

Dimi
"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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 SelectedIndexCh anged 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(objec t sender, System.EventArg s e)

{

if(!Page.IsPost Back)

{

DropDownList tempDDL = new DropDownList();

tempDDL.Selecte dIndexChanged += new
System.EventHan dler(DDL_Select edIndexChanged) ;

tempDDL.AutoPos tBack = true;

phDropDowns.Con trols.Add(tempD DL);

}
}
"Dimitrios Toulakis" <ma*********@we b.de> wrote in message
news:u5******** ******@TK2MSFTN GP14.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.SelectedIn dexChanged, AddressOf listClick

End Sub

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

'add your code here

End Sub

"Sosh" <so*******@some thing.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
> Hi,
>
> I can't seem to find much information on this. Please could someone
> explain to me how to wire up events (selectedIndexC hanged) 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
3272
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 be wired up? All the examples I've seen wire up the event programatically, thus making this declarative attribute irrelevant. e.g.
0
1642
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 is defined within an aspx page <MyTag:MyControl id="MyControl1" runat="server">: MyControl handle succesfully events raised from its children. When I try to create dynamically MyControl and to Add it to another control, event handling doesn't work...
0
1138
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 handler sets up a member variable that needs to control how the dymanic controls are created in the first place! Alas, I can't do so because it's already too late since the Load event fires before the Click event.
6
3383
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
4
3028
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, as needed, I'm having trouble wiring up its click event procedure. The problem is that when I go to subscribe the ImageButton to the delegate, the ImageButton is <undefined> (i.e., throws the "object not found" exception). Note that I'm using...
1
1410
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 Bill
5
3079
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 and the specific type of control frequently changes. I have a common event in the base class that each child user control inherits from and that the page wires up and uses to get status from the child user control. I hope all pretty simple and not...
18
5436
by: Redhairs | last post by:
Is it possible to get DropDownList.SelectedValue in Page_PreInit() event during the postback?
0
2944
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 that the controls are loaded/unloaded based on the SelectionChanged event for the tab strip and again in Page load because with a dynamic load viewstate has to be reloaded. I have a datalist in the user control and I am trying to create the...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.