Result not expected with user control - any ideas why/

S

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
 
P

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)
 
S

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
 
S

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
 
P

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.
 
S

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top