G
Guest
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
**********************************
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
**********************************