Home All Groups Group Topic Archive Search About

Result not expected with user control - any ideas why/

Author
12 Dec 2005 7:05 PM
Sue
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

Author
12 Dec 2005 7:38 PM
Phillip Williams
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)
Show quoteHide quote
"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
Author
12 Dec 2005 7:49 PM
Sue
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

Show quoteHide quote
"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
Author
12 Dec 2005 8:00 PM
Sue
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

Show quoteHide quote
"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
Author
12 Dec 2005 9:08 PM
Phillip Williams
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.

Show quoteHide quote
"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
Author
12 Dec 2005 9:23 PM
Sue
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

Show quoteHide quote
"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
Author
12 Dec 2005 9:37 PM
Phillip Williams
You are welcome. :-)
Show quoteHide quote
"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