Event fails to fire first time in dynamically loaded user control(

Discussion in 'ASP .Net' started by =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 5, 2005.

  1. 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!
    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 5, 2005
    #1
    1. Advertising

  2. 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!
    >
    >
    >
    =?Utf-8?B?UGF0cmljaw==?=, Apr 5, 2005
    #2
    1. Advertising

  3. RE: Event fails to fire first time in dynamically loaded user cont

    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!
    > >
    > >
    > >
    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 5, 2005
    #3
  4. RE: Event fails to fire first time in dynamically loaded user cont

    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!
    > > >
    > > >
    > > >
    =?Utf-8?B?UGF0cmljaw==?=, Apr 6, 2005
    #4
  5. RE: Event fails to fire first time in dynamically loaded user cont

    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!
    > > > >
    > > > >
    > > > >
    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 6, 2005
    #5
  6. RE: Event fails to fire first time in dynamically loaded user cont

    Hi Steve,

    for me, it is clear that it is the "Button1.Click" event which, when fired,
    adds dynamically an usercontrol. It really has something to do with the
    uniqueID, because ASP.Net can only create that ID based on a valid
    control-hierarchy and this hierarchy MUST persist upon PostBack. That means,
    the clicked button does not have the same ID when it checks whether to fire
    the click event or not. You must imagin, the button just does a request of
    it's UniqueID; if it's set to a value the button was clicked otherwise not.

    All you have to do is to put the code, where the usercontrol is loaded, into
    the Init event of your Page. Look here:

    Public Class WebForm1

    ....

    Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Init

    PlaceHolder1.Controls.Clear ()
    If (Session ("w1") Is Not Nothing) Then
    PlaceHolder1.Controls.Add (LoadControl ("~/MyUserControl1.ascx"))
    ElseIf (Session ("w2") Is Not Nothing) Then
    PlaceHolder1.Controls.Add (LoadControl ("~/MyUserControl2.ascx"))
    ' ElseIf ... And so on ...
    End If

    ....

    End Sub

    End Class

    Or, even better, into the InitializeComponent. Because that's what it's for ;)
    However, loading controls dynamically in a Click event of a button is
    generally a bad idea; except you know exactly(!) what you are doing.

    Hope that helps
    Patrick

    "Steve Booth" wrote:

    > 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!
    > > > > >
    > > > > >
    > > > > >
    =?Utf-8?B?UGF0cmljaw==?=, Apr 6, 2005
    #6
  7. RE: Event fails to fire first time in dynamically loaded user cont

    Hi Patrick

    Not at work at present to check this out but (from memory) the main problem
    is that when selecting a button to change the page layout the PageInit and
    PageLoad events are fired before the button event. On clearing/adding the new
    control(s) in the button event the page load for the new controls is fired -
    this must be when it gets the wrong uniqueid (ie mismatches the current
    hierarchy).

    Interestingly - though i did not mention it in the post because i thought it
    would it too confusing - i do have a case where this does work! I am have
    tried very hard to determine the difference but could not. But this was
    before the uniqueid possibility was raised. I look forward to getting to work
    tomorrow to look at this!

    At the limit i shall raise an incident with Microsoft - about £70 by email
    and probably worth it.


    Regards
    Steve


    "Patrick" wrote:

    > Hi Steve,
    >
    > for me, it is clear that it is the "Button1.Click" event which, when fired,
    > adds dynamically an usercontrol. It really has something to do with the
    > uniqueID, because ASP.Net can only create that ID based on a valid
    > control-hierarchy and this hierarchy MUST persist upon PostBack. That means,
    > the clicked button does not have the same ID when it checks whether to fire
    > the click event or not. You must imagin, the button just does a request of
    > it's UniqueID; if it's set to a value the button was clicked otherwise not.
    >
    > All you have to do is to put the code, where the usercontrol is loaded, into
    > the Init event of your Page. Look here:
    >
    > Public Class WebForm1
    >
    > ...
    >
    > Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    > Handles MyBase.Init
    >
    > PlaceHolder1.Controls.Clear ()
    > If (Session ("w1") Is Not Nothing) Then
    > PlaceHolder1.Controls.Add (LoadControl ("~/MyUserControl1.ascx"))
    > ElseIf (Session ("w2") Is Not Nothing) Then
    > PlaceHolder1.Controls.Add (LoadControl ("~/MyUserControl2.ascx"))
    > ' ElseIf ... And so on ...
    > End If
    >
    > ...
    >
    > End Sub
    >
    > End Class
    >
    > Or, even better, into the InitializeComponent. Because that's what it's for ;)
    > However, loading controls dynamically in a Click event of a button is
    > generally a bad idea; except you know exactly(!) what you are doing.
    >
    > Hope that helps
    > Patrick
    >
    > "Steve Booth" wrote:
    >
    > > 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!
    > > > > > >
    > > > > > >
    > > > > > >
    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 6, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    1
    Views:
    7,679
    nfedin
    Feb 10, 2004
  2. Thanh Nguyen
    Replies:
    0
    Views:
    427
    Thanh Nguyen
    Apr 1, 2004
  3. Tonix
    Replies:
    0
    Views:
    403
    Tonix
    Aug 24, 2004
  4. Alan Silver
    Replies:
    2
    Views:
    431
    Alan Silver
    Feb 24, 2005
  5. Tonix
    Replies:
    0
    Views:
    163
    Tonix
    Aug 24, 2004
Loading...

Share This Page