Dear Patrick
I have pasted the code below, hope it comes out alright i am new to this.....!
I am coming to the conclusion it may be something to do with the uniqueid,
which i am going to investigate now.
The form code is:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb"
Inherits="fracas.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder id="PlaceHolder1" runat="server"
EnableViewState="False"></asp:PlaceHolder>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 15px; POSITION:
absolute; TOP: 64px" runat="server"
Text="Show Control 2"></asp:Button>
</form>
</body>
</HTML>
Public Class WebForm2
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'If Session("w3") Then
' Dim ctl As WebUserControl3 = LoadControl("WebUserControl3.ascx")
' PlaceHolder1.Controls.Clear()
' PlaceHolder1.Controls.Add(ctl)
'ElseIf Session("w2") Then
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
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
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
End Class
The user control code is :
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="WebUserControl2.ascx.vb" Inherits="fracas.WebUserControl2"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:Button id="Button1" Text="Web User Control 2"
runat="server"></asp:Button>
Public Class WebUserControl2
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
End Sub
End Class
Regards
Steve Booth
"Patrick" wrote:
Hi Steve,
how do you decide which control to load and where? Is this decision
dependant on any postback data?
regards
patrick
"Steve Booth" wrote:
Dear Patrick
I'm afraid i have tried this already. I have been loading the user control
in OnInit (and tried it almost everywhere else - to no avail.
Regards
Steve
"Patrick" wrote:
Hi Steve,
I think, this is a "time" problem. The Button's event can only be fired,
when it was recognized. If you load the control, after the RaisePostBackEvent
should have been called the control will never find out, whether s.o. clicked
on it or not.
Try to add the control before "OnPreRender", the OnLoad method is a good
place for the code. Just to check if it's this problem.
cu patrick
"Steve Booth" wrote:
> 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 user control again
> - select user control button, event fails to fire (but postback takes place)
> - select user control button again event fires this time
>
> This started as a more complex example with a two 'main' buttons showing and
> hiding two user controls but i have reduced it down to this simple example.
>
> Has anyone any ideas on this - i would hate to be reduced to statically
> creating the user controls and showing/hiding them!
>
>
>