473,402 Members | 2,072 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,402 software developers and data experts.

Dynamic load of user control, problem with child controls event fi

I have a web form containing a button that when selected adds a user control
to a place holder. The user control contains a button. The first time the
user control is added the contained button fires Click event first time it is
selected. The second (andd successive) times the user control is added the
button Click event does not fire the first time, only on the second and
successive times.

This problem is caused by the naming of the controls. The second time the
user control is added it appears that the naming container still know about
the old control deleted from the placeholder when it generates the name for
the new added control (which appears to thus get the wrong name). So how do
i force it to have the correct name?

The code below is a crude version of the full code that is causing me the
problem!

The web form code:

**************************
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ctl As WebUserControl2 = LoadControl("WebUserControl2.ascx")

PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(ctl)

Session("w2") = True

End Sub
***************

The user control is added on page init:

********************
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

If Session("w2") Then
Dim ctl As WebUserControl2 = LoadControl("WebUserControl2.ascx")

PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(ctl)

End If

MyBase.OnInit(e)

End Sub
***************************
The user control contains a button that when selected writes text to a
textbox:

********************************
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextBox1.Text = "Button clicked"

End Sub
**********************************
Nov 19 '05 #1
1 2142
Try using Denis Bauer's dynamic placeholder control (free):
http://www.denisbauer.com/ASPNETCont...aceholder.aspx

It takes care of dynamic control recreation across postback, which means you
woudlnt' need to recreate the control yourslef on init and use sessions. I
think it'll solve your problem.

karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Steve Booth" <St********@discussions.microsoft.com> wrote in message
news:8A**********************************@microsof t.com...
I have a web form containing a button that when selected adds a user
control
to a place holder. The user control contains a button. The first time the
user control is added the contained button fires Click event first time it
is
selected. The second (andd successive) times the user control is added
the
button Click event does not fire the first time, only on the second and
successive times.

This problem is caused by the naming of the controls. The second time the
user control is added it appears that the naming container still know
about
the old control deleted from the placeholder when it generates the name
for
the new added control (which appears to thus get the wrong name). So how
do
i force it to have the correct name?

The code below is a crude version of the full code that is causing me the
problem!

The web form code:

**************************
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ctl As WebUserControl2 = LoadControl("WebUserControl2.ascx")

PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(ctl)

Session("w2") = True

End Sub
***************

The user control is added on page init:

********************
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

If Session("w2") Then
Dim ctl As WebUserControl2 = LoadControl("WebUserControl2.ascx")

PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(ctl)

End If

MyBase.OnInit(e)

End Sub
***************************
The user control contains a button that when selected writes text to a
textbox:

********************************
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextBox1.Text = "Button clicked"

End Sub
**********************************

Nov 19 '05 #2

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

Similar topics

9
by: wASP | last post by:
Hello again to all of you geniuses, I'm having a problem trying to load dynamic controls at the initialization phase. I've read the docs, and I thought I had it figured out:...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
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...
0
by: davidr | last post by:
Hi, I have a panel that I load user Control in no problem. The problem arrises when I do a post back on one of these user controls. I have button it does a click event. In this click event I...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
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: =?Utf-8?B?Y2luZHk=?= | 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...
3
balabaster
by: balabaster | last post by:
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...
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.