473,607 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim ctl As WebUserControl2 = LoadControl("We bUserControl2.a scx")

PlaceHolder1.Co ntrols.Clear()
PlaceHolder1.Co ntrols.Add(ctl)

Session("w2") = True

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

The user control is added on page init:

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

If Session("w2") Then
Dim ctl As WebUserControl2 = LoadControl("We bUserControl2.a scx")

PlaceHolder1.Co ntrols.Clear()
PlaceHolder1.Co ntrols.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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click

TextBox1.Text = "Button clicked"

End Sub
*************** *************** ****
Nov 19 '05 #1
1 2150
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********@dis cussions.micros oft.com> wrote in message
news:8A******** *************** ***********@mic rosoft.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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim ctl As WebUserControl2 = LoadControl("We bUserControl2.a scx")

PlaceHolder1.Co ntrols.Clear()
PlaceHolder1.Co ntrols.Add(ctl)

Session("w2") = True

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

The user control is added on page init:

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

If Session("w2") Then
Dim ctl As WebUserControl2 = LoadControl("We bUserControl2.a scx")

PlaceHolder1.Co ntrols.Clear()
PlaceHolder1.Co ntrols.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(B yVal sender As Object, ByVal e As
System.EventArg s) 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
17612
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: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> .NET Framework Class Library Control.Init Event
1
7559
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 dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
3
3966
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 step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
5
2401
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 control (.ascx).
0
3414
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 will do some database updates, then I will use reflection to call a method in the parent (this.page) to load the new user control. Its at this point after it loads the new user control that it throws the error "The control must be placed inside...
4
51639
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 controls need to be dynamically loaded according to user input. This simple example can be further extended to dynamically load custom web user controls. Background Please familiarize yourself with the ASP.NET Page Life Cycle. It is crucial to...
0
2933
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
1907
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 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...
3
4356
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 number of dynamic controls that themselves have postback/autopostback so the display of the control needs to be built at latest in the Page.Load event or the event handlers for the controls don't get wired up. Now if I have a page that uses this user...
0
8472
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
8464
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...
0
8324
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...
0
6805
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6000
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...
0
3954
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4015
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2464
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
0
1318
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.