473,320 Members | 1,940 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,320 software developers and data experts.

How to handle Dynamic Controls within User Controls

Howdy, I think I have a good question!

I'm creating a usercontrol, and within this usercontrol, I'm dynamically
creating controls (imagebuttons to be exact), that the user can click on
which will cause a postback to occur.

My problem is, I can't seem to be able to get the handlers to work within
the user control, to determine which control has been clicked, and what to
do with it from there.

I want these events to be handled within the user control itself, and not
the page that has the user control.

Here is what I've been doing:

'I'm adding this imagebutton to a tablecell I'm dynamically creating as
well, for simplicity i'm not showing that this code is part of a for each
loop, and it can be ran many times...

Dim dImage as ImageButton = new ImageButton

AddHandler dImage.click, AddressOf DeleteButton_Click
dImage.imageurl = "../images/delete.gif"
dImage.tooltip = "Delete this Item"
tblcellDelete.Controls.Add(dImage) 'tcDelete is a TableCell
tblRow.Controls.Add(tcDelete) 'trow is a TableRow
tblShow.Controls.Add(trow) 'tblShow is a Table

Then I create the event handler in the user control

Sub DeleteButton_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
Response.Write("clicked an image?")
End Sub

For now I'm just using Response.write with a breakpoint on that line to see
if that code executes. Unfortunately the DeleteButton_Click Sub is never
called, the page just posts back. Can someone tell me the proper way to
handle events for dynamically created controls within user controls?

Thanks!
--Michael
Nov 18 '05 #1
2 4207
I've done a similar thing myself, emphasis on SIMILAR.
I fix my similar problem I had to reload all of the dynamic controls on each
postback, the viewstate info then allowed the events to take place.

Hope that helps
"Michael Ramey" <raterus@localhost> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
Howdy, I think I have a good question!

I'm creating a usercontrol, and within this usercontrol, I'm dynamically
creating controls (imagebuttons to be exact), that the user can click on
which will cause a postback to occur.

My problem is, I can't seem to be able to get the handlers to work within
the user control, to determine which control has been clicked, and what to
do with it from there.

I want these events to be handled within the user control itself, and not
the page that has the user control.

Here is what I've been doing:

'I'm adding this imagebutton to a tablecell I'm dynamically creating as
well, for simplicity i'm not showing that this code is part of a for each
loop, and it can be ran many times...

Dim dImage as ImageButton = new ImageButton

AddHandler dImage.click, AddressOf DeleteButton_Click
dImage.imageurl = "../images/delete.gif"
dImage.tooltip = "Delete this Item"
tblcellDelete.Controls.Add(dImage) 'tcDelete is a TableCell
tblRow.Controls.Add(tcDelete) 'trow is a TableRow
tblShow.Controls.Add(trow) 'tblShow is a Table

Then I create the event handler in the user control

Sub DeleteButton_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
Response.Write("clicked an image?")
End Sub

For now I'm just using Response.write with a breakpoint on that line to see if that code executes. Unfortunately the DeleteButton_Click Sub is never
called, the page just posts back. Can someone tell me the proper way to
handle events for dynamically created controls within user controls?

Thanks!
--Michael

Nov 18 '05 #2
Worked great, just one thing, the button i'm writing the event for is a
"delete" button, so on a postback, I wouldn't want it to appear again. I
suppose I can search and delete the control before it is rendered again.
Thanks for the info, it did help.

"Felbrigg" <so*****@microsoft.com> wrote in message
news:Od****************@tk2msftngp13.phx.gbl...
I've done a similar thing myself, emphasis on SIMILAR.
I fix my similar problem I had to reload all of the dynamic controls on each postback, the viewstate info then allowed the events to take place.

Hope that helps
"Michael Ramey" <raterus@localhost> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
Howdy, I think I have a good question!

I'm creating a usercontrol, and within this usercontrol, I'm dynamically
creating controls (imagebuttons to be exact), that the user can click on
which will cause a postback to occur.

My problem is, I can't seem to be able to get the handlers to work within the user control, to determine which control has been clicked, and what to do with it from there.

I want these events to be handled within the user control itself, and not the page that has the user control.

Here is what I've been doing:

'I'm adding this imagebutton to a tablecell I'm dynamically creating as
well, for simplicity i'm not showing that this code is part of a for each loop, and it can be ran many times...

Dim dImage as ImageButton = new ImageButton

AddHandler dImage.click, AddressOf DeleteButton_Click
dImage.imageurl = "../images/delete.gif"
dImage.tooltip = "Delete this Item"
tblcellDelete.Controls.Add(dImage) 'tcDelete is a TableCell
tblRow.Controls.Add(tcDelete) 'trow is a TableRow
tblShow.Controls.Add(trow) 'tblShow is a Table

Then I create the event handler in the user control

Sub DeleteButton_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
Response.Write("clicked an image?")
End Sub

For now I'm just using Response.write with a breakpoint on that line to

see
if that code executes. Unfortunately the DeleteButton_Click Sub is never called, the page just posts back. Can someone tell me the proper way to
handle events for dynamically created controls within user controls?

Thanks!
--Michael


Nov 18 '05 #3

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

Similar topics

1
by: Steve Gadlin | last post by:
Hi there! First off, let me apologize for the basic question... I'm very new to .NET programming. I'm building a web site using VB.NET, and am trying to include several custom user controls. ...
4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
2
by: theComputer7 | last post by:
I cut down the code to make this half way understandable... I have read Data Grid girls caution about over use of dynamic controls. I truly believe what I am doing requires dynamically inserted...
1
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
2
by: Michael | last post by:
Need some help trying to read values from web controls - specifically *finding* the controls (like a drop down list) - that are added dynamically added within an asp:panel control. The page...
5
by: mytestemailaccount | last post by:
Hi, Hope you can help. I am relatively new to all this but would appreciate the groups help. The scenario: I am c# and asp.net to create a web application. The web page contains a user...
7
by: John | last post by:
Hi all, I need finality on this once and for all please. I have a main page which contains a couple of placeholders and within these placeholders, depending on what the user presses, I load...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.