Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   Result not expected with user control - any ideas why/ (http://www.velocityreviews.com/forums/t777381-result-not-expected-with-user-control-any-ideas-why.html)

Sue 12-12-2005 07:05 PM

Result not expected with user control - any ideas why/
 
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 12-12-2005 07:38 PM

RE: Result not expected with user control - any ideas why/
 
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 12-12-2005 07:49 PM

RE: Result not expected with user control - any ideas why/
 
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 12-12-2005 08:00 PM

RE: Result not expected with user control - any ideas why/
 
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 12-12-2005 09:08 PM

RE: Result not expected with user control - any ideas why/
 
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 12-12-2005 09:23 PM

RE: Result not expected with user control - any ideas why/
 
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 12-12-2005 09:37 PM

RE: Result not expected with user control - any ideas why/
 
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



All times are GMT. The time now is 11:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.