Result not expected with user control - any ideas why/

Discussion in 'ASP .Net Web Controls' started by Sue, Dec 12, 2005.

  1. Sue

    Sue Guest

    aspx:

    <form id="Form1" method="post" runat="server">
    <asp:placeHolder ID="PH" Runat="server"/>
    </form>
    '-----------------------------------
    code behind:

    Protected WithEvents PH As PlaceHolder
    Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    TestLabel.SetText = "Hello World"
    PH.Controls.Add(TestLabel)

    End Sub
    '-----------------------------
    DisplayLabels.ascx:

    <asp:Label ID="ThisLabel" Runat="server" />
    '----------------------------
    code behind:

    Protected WithEvents ThisLabel As Label

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    With ThisLabel
    .ID = "ThisLabel"
    .Text = "None"
    End With

    End Sub

    Public WriteOnly Property SetText() As String
    Set(ByVal Value As String)
    ThisLabel.Text = Trim(Value)
    End Set
    End Property

    '-------------------------

    when compiled and brought up in browser (IE6.x), the result is "None" rather
    than "Hello World". Any clue why?

    tia,
    Sue
    Sue, Dec 12, 2005
    #1
    1. Advertising

  2. Hi Sue,

    The Page_Load event of the container webform is fired before the Page_Load
    event of the loaded control. (You can see it by setting break points while
    debugging)
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Sue" wrote:

    > aspx:
    >
    > <form id="Form1" method="post" runat="server">
    > <asp:placeHolder ID="PH" Runat="server"/>
    > </form>
    > '-----------------------------------
    > code behind:
    >
    > Protected WithEvents PH As PlaceHolder
    > Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > TestLabel.SetText = "Hello World"
    > PH.Controls.Add(TestLabel)
    >
    > End Sub
    > '-----------------------------
    > DisplayLabels.ascx:
    >
    > <asp:Label ID="ThisLabel" Runat="server" />
    > '----------------------------
    > code behind:
    >
    > Protected WithEvents ThisLabel As Label
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > With ThisLabel
    > .ID = "ThisLabel"
    > .Text = "None"
    > End With
    >
    > End Sub
    >
    > Public WriteOnly Property SetText() As String
    > Set(ByVal Value As String)
    > ThisLabel.Text = Trim(Value)
    > End Set
    > End Property
    >
    > '-------------------------
    >
    > when compiled and brought up in browser (IE6.x), the result is "None" rather
    > than "Hello World". Any clue why?
    >
    > tia,
    > Sue
    Phillip Williams, Dec 12, 2005
    #2
    1. Advertising

  3. Sue

    Sue Guest

    Phillip, still not getting what I expect. I moved the following to the
    Page_Init of the container webform:

    Dim TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    TestLabel.SetText = "Hello World"
    PH.Controls.Add(TestLabel)

    and stepped through the code in debug. The above executed before the
    Page_Load of the test.aspx. Still got "None" in the browser. Am I missing
    something?

    tia,
    Sue

    "Phillip Williams" wrote:

    > Hi Sue,
    >
    > The Page_Load event of the container webform is fired before the Page_Load
    > event of the loaded control. (You can see it by setting break points while
    > debugging)
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Sue" wrote:
    >
    > > aspx:
    > >
    > > <form id="Form1" method="post" runat="server">
    > > <asp:placeHolder ID="PH" Runat="server"/>
    > > </form>
    > > '-----------------------------------
    > > code behind:
    > >
    > > Protected WithEvents PH As PlaceHolder
    > > Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > TestLabel.SetText = "Hello World"
    > > PH.Controls.Add(TestLabel)
    > >
    > > End Sub
    > > '-----------------------------
    > > DisplayLabels.ascx:
    > >
    > > <asp:Label ID="ThisLabel" Runat="server" />
    > > '----------------------------
    > > code behind:
    > >
    > > Protected WithEvents ThisLabel As Label
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > With ThisLabel
    > > .ID = "ThisLabel"
    > > .Text = "None"
    > > End With
    > >
    > > End Sub
    > >
    > > Public WriteOnly Property SetText() As String
    > > Set(ByVal Value As String)
    > > ThisLabel.Text = Trim(Value)
    > > End Set
    > > End Property
    > >
    > > '-------------------------
    > >
    > > when compiled and brought up in browser (IE6.x), the result is "None" rather
    > > than "Hello World". Any clue why?
    > >
    > > tia,
    > > Sue
    Sue, Dec 12, 2005
    #3
  4. Sue

    Sue Guest

    Ok, I see what it's doing. If I take the "default" text of "None" out of the
    page_load of the user control, it works fine. Which begs the question of
    where to set up the default text....Hmm...

    Thanks, Phillip!
    Sue

    "Phillip Williams" wrote:

    > Hi Sue,
    >
    > The Page_Load event of the container webform is fired before the Page_Load
    > event of the loaded control. (You can see it by setting break points while
    > debugging)
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Sue" wrote:
    >
    > > aspx:
    > >
    > > <form id="Form1" method="post" runat="server">
    > > <asp:placeHolder ID="PH" Runat="server"/>
    > > </form>
    > > '-----------------------------------
    > > code behind:
    > >
    > > Protected WithEvents PH As PlaceHolder
    > > Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > TestLabel.SetText = "Hello World"
    > > PH.Controls.Add(TestLabel)
    > >
    > > End Sub
    > > '-----------------------------
    > > DisplayLabels.ascx:
    > >
    > > <asp:Label ID="ThisLabel" Runat="server" />
    > > '----------------------------
    > > code behind:
    > >
    > > Protected WithEvents ThisLabel As Label
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > With ThisLabel
    > > .ID = "ThisLabel"
    > > .Text = "None"
    > > End With
    > >
    > > End Sub
    > >
    > > Public WriteOnly Property SetText() As String
    > > Set(ByVal Value As String)
    > > ThisLabel.Text = Trim(Value)
    > > End Set
    > > End Property
    > >
    > > '-------------------------
    > >
    > > when compiled and brought up in browser (IE6.x), the result is "None" rather
    > > than "Hello World". Any clue why?
    > >
    > > tia,
    > > Sue
    Sue, Dec 12, 2005
    #4
  5. Hi Sue,

    The control load event is fired after the step where added the control to
    the placeholder. So to get your default value, move the code that you placed
    in the Page_Load to the Page_Init, e.g.

    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()
    TestLabel.SetText = "Hello World"
    PH.Controls.Add(TestLabel)
    End Sub

    You could achieve the exact same effect declaratively (without adding any
    code behind, ie. in the Page_Load event of your control) like this:

    1- add the following tag before your page directive:
    <%@ Register TagPrefix="UC" tagName="TestLabel" src="DisplayLabels.ascx" %>

    2- add the following markup within the page:
    <UC:TestLabel id="TestLabel1" runat=server SetText="Default
    Text"></UC:TestLabel>

    3- remove the code lines from the webform that loads the control and add it
    to the placeholder.

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Sue" wrote:

    > Ok, I see what it's doing. If I take the "default" text of "None" out of the
    > page_load of the user control, it works fine. Which begs the question of
    > where to set up the default text....Hmm...
    >
    > Thanks, Phillip!
    > Sue
    >
    > "Phillip Williams" wrote:
    >
    > > Hi Sue,
    > >
    > > The Page_Load event of the container webform is fired before the Page_Load
    > > event of the loaded control. (You can see it by setting break points while
    > > debugging)
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "Sue" wrote:
    > >
    > > > aspx:
    > > >
    > > > <form id="Form1" method="post" runat="server">
    > > > <asp:placeHolder ID="PH" Runat="server"/>
    > > > </form>
    > > > '-----------------------------------
    > > > code behind:
    > > >
    > > > Protected WithEvents PH As PlaceHolder
    > > > Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    > > >
    > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Load
    > > >
    > > > TestLabel.SetText = "Hello World"
    > > > PH.Controls.Add(TestLabel)
    > > >
    > > > End Sub
    > > > '-----------------------------
    > > > DisplayLabels.ascx:
    > > >
    > > > <asp:Label ID="ThisLabel" Runat="server" />
    > > > '----------------------------
    > > > code behind:
    > > >
    > > > Protected WithEvents ThisLabel As Label
    > > >
    > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Load
    > > >
    > > > With ThisLabel
    > > > .ID = "ThisLabel"
    > > > .Text = "None"
    > > > End With
    > > >
    > > > End Sub
    > > >
    > > > Public WriteOnly Property SetText() As String
    > > > Set(ByVal Value As String)
    > > > ThisLabel.Text = Trim(Value)
    > > > End Set
    > > > End Property
    > > >
    > > > '-------------------------
    > > >
    > > > when compiled and brought up in browser (IE6.x), the result is "None" rather
    > > > than "Hello World". Any clue why?
    > > >
    > > > tia,
    > > > Sue
    Phillip Williams, Dec 12, 2005
    #5
  6. Sue

    Sue Guest

    There are days when I think I should wear a paper bag over my head in shame -
    now that I see it, this one is a no-brainer. Of course moving the code to the
    page_init would set and keep the default. Sigh. Thank you Phillip for saving
    me from myself. I'm going to go find some M&Ms and sit in the closet for a
    while....

    Sue

    "Phillip Williams" wrote:

    > Hi Sue,
    >
    > The control load event is fired after the step where added the control to
    > the placeholder. So to get your default value, move the code that you placed
    > in the Page_Load to the Page_Init, e.g.
    >
    > 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()
    > TestLabel.SetText = "Hello World"
    > PH.Controls.Add(TestLabel)
    > End Sub
    >
    > You could achieve the exact same effect declaratively (without adding any
    > code behind, ie. in the Page_Load event of your control) like this:
    >
    > 1- add the following tag before your page directive:
    > <%@ Register TagPrefix="UC" tagName="TestLabel" src="DisplayLabels.ascx" %>
    >
    > 2- add the following markup within the page:
    > <UC:TestLabel id="TestLabel1" runat=server SetText="Default
    > Text"></UC:TestLabel>
    >
    > 3- remove the code lines from the webform that loads the control and add it
    > to the placeholder.
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Sue" wrote:
    >
    > > Ok, I see what it's doing. If I take the "default" text of "None" out of the
    > > page_load of the user control, it works fine. Which begs the question of
    > > where to set up the default text....Hmm...
    > >
    > > Thanks, Phillip!
    > > Sue
    > >
    > > "Phillip Williams" wrote:
    > >
    > > > Hi Sue,
    > > >
    > > > The Page_Load event of the container webform is fired before the Page_Load
    > > > event of the loaded control. (You can see it by setting break points while
    > > > debugging)
    > > > --
    > > > HTH,
    > > > Phillip Williams
    > > > http://www.societopia.net
    > > > http://www.webswapp.com
    > > >
    > > >
    > > > "Sue" wrote:
    > > >
    > > > > aspx:
    > > > >
    > > > > <form id="Form1" method="post" runat="server">
    > > > > <asp:placeHolder ID="PH" Runat="server"/>
    > > > > </form>
    > > > > '-----------------------------------
    > > > > code behind:
    > > > >
    > > > > Protected WithEvents PH As PlaceHolder
    > > > > Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    > > > >
    > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > > System.EventArgs) Handles MyBase.Load
    > > > >
    > > > > TestLabel.SetText = "Hello World"
    > > > > PH.Controls.Add(TestLabel)
    > > > >
    > > > > End Sub
    > > > > '-----------------------------
    > > > > DisplayLabels.ascx:
    > > > >
    > > > > <asp:Label ID="ThisLabel" Runat="server" />
    > > > > '----------------------------
    > > > > code behind:
    > > > >
    > > > > Protected WithEvents ThisLabel As Label
    > > > >
    > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > > System.EventArgs) Handles MyBase.Load
    > > > >
    > > > > With ThisLabel
    > > > > .ID = "ThisLabel"
    > > > > .Text = "None"
    > > > > End With
    > > > >
    > > > > End Sub
    > > > >
    > > > > Public WriteOnly Property SetText() As String
    > > > > Set(ByVal Value As String)
    > > > > ThisLabel.Text = Trim(Value)
    > > > > End Set
    > > > > End Property
    > > > >
    > > > > '-------------------------
    > > > >
    > > > > when compiled and brought up in browser (IE6.x), the result is "None" rather
    > > > > than "Hello World". Any clue why?
    > > > >
    > > > > tia,
    > > > > Sue
    Sue, Dec 12, 2005
    #6
  7. You are welcome. :)
    --
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Sue" wrote:

    > There are days when I think I should wear a paper bag over my head in shame -
    > now that I see it, this one is a no-brainer. Of course moving the code to the
    > page_init would set and keep the default. Sigh. Thank you Phillip for saving
    > me from myself. I'm going to go find some M&Ms and sit in the closet for a
    > while....
    >
    > Sue
    >
    > "Phillip Williams" wrote:
    >
    > > Hi Sue,
    > >
    > > The control load event is fired after the step where added the control to
    > > the placeholder. So to get your default value, move the code that you placed
    > > in the Page_Load to the Page_Init, e.g.
    > >
    > > 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()
    > > TestLabel.SetText = "Hello World"
    > > PH.Controls.Add(TestLabel)
    > > End Sub
    > >
    > > You could achieve the exact same effect declaratively (without adding any
    > > code behind, ie. in the Page_Load event of your control) like this:
    > >
    > > 1- add the following tag before your page directive:
    > > <%@ Register TagPrefix="UC" tagName="TestLabel" src="DisplayLabels.ascx" %>
    > >
    > > 2- add the following markup within the page:
    > > <UC:TestLabel id="TestLabel1" runat=server SetText="Default
    > > Text"></UC:TestLabel>
    > >
    > > 3- remove the code lines from the webform that loads the control and add it
    > > to the placeholder.
    > >
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "Sue" wrote:
    > >
    > > > Ok, I see what it's doing. If I take the "default" text of "None" out of the
    > > > page_load of the user control, it works fine. Which begs the question of
    > > > where to set up the default text....Hmm...
    > > >
    > > > Thanks, Phillip!
    > > > Sue
    > > >
    > > > "Phillip Williams" wrote:
    > > >
    > > > > Hi Sue,
    > > > >
    > > > > The Page_Load event of the container webform is fired before the Page_Load
    > > > > event of the loaded control. (You can see it by setting break points while
    > > > > debugging)
    > > > > --
    > > > > HTH,
    > > > > Phillip Williams
    > > > > http://www.societopia.net
    > > > > http://www.webswapp.com
    > > > >
    > > > >
    > > > > "Sue" wrote:
    > > > >
    > > > > > aspx:
    > > > > >
    > > > > > <form id="Form1" method="post" runat="server">
    > > > > > <asp:placeHolder ID="PH" Runat="server"/>
    > > > > > </form>
    > > > > > '-----------------------------------
    > > > > > code behind:
    > > > > >
    > > > > > Protected WithEvents PH As PlaceHolder
    > > > > > Protected WithEvents TestLabel As TestLabels = LoadControl("TestLabels.ascx")
    > > > > >
    > > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > > > System.EventArgs) Handles MyBase.Load
    > > > > >
    > > > > > TestLabel.SetText = "Hello World"
    > > > > > PH.Controls.Add(TestLabel)
    > > > > >
    > > > > > End Sub
    > > > > > '-----------------------------
    > > > > > DisplayLabels.ascx:
    > > > > >
    > > > > > <asp:Label ID="ThisLabel" Runat="server" />
    > > > > > '----------------------------
    > > > > > code behind:
    > > > > >
    > > > > > Protected WithEvents ThisLabel As Label
    > > > > >
    > > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > > > System.EventArgs) Handles MyBase.Load
    > > > > >
    > > > > > With ThisLabel
    > > > > > .ID = "ThisLabel"
    > > > > > .Text = "None"
    > > > > > End With
    > > > > >
    > > > > > End Sub
    > > > > >
    > > > > > Public WriteOnly Property SetText() As String
    > > > > > Set(ByVal Value As String)
    > > > > > ThisLabel.Text = Trim(Value)
    > > > > > End Set
    > > > > > End Property
    > > > > >
    > > > > > '-------------------------
    > > > > >
    > > > > > when compiled and brought up in browser (IE6.x), the result is "None" rather
    > > > > > than "Hello World". Any clue why?
    > > > > >
    > > > > > tia,
    > > > > > Sue
    Phillip Williams, Dec 12, 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. David Hearn
    Replies:
    4
    Views:
    333
    David Hearn
    May 4, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  3. Michael Tan
    Replies:
    32
    Views:
    937
    Ara.T.Howard
    Jul 21, 2005
  4. Richard Lionheart
    Replies:
    2
    Views:
    179
    Richard Lionheart
    Jan 28, 2006
  5. Replies:
    3
    Views:
    354
Loading...

Share This Page