Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: The loading and programmatic access of UserControls from WebForms


Message #1 by "Patrick Barnes" <patrick@c...> on Thu, 5 Apr 2001 00:51:02 -0500
I am trying to get a handle on how to set and access properties of a

UserControl from a WebForm that consumes the UserControl. From what I can

tell by looking at past posts and from talking with others, there is a lot

of interest in this area but few answers.



I've been experimenting tonite and am hoping by starting this thread I can

stimulate some good discussion on this topic. To start off, I've created a

simple WebForm and UserControl. All I want to do at this point is to set a

Property in the UserControl from the WebForm's Page Load event. Seems simple

enough, but I cannot figure out how to keep the UserControl from showing up

twice on the rendered page. And it raises some questions about the way .NET

compiles and renders a WebForm.



Here's the key blocks of code, and then some questions/observations follow:



===========  WEBFORM CODEBEHIND ==============



    Protected Sub WebForm1_Load(ByVal Sender As System.Object, ByVal e As

System.EventArgs)

        Trace.Write("webform1", "Entering webform1_Load")

        Response.Write("This page will tell you my name...maybe:<br><p>")



        If Not IsPostback Then   ' Evals true first time browser hits the

page

            Dim myControl As Control = Page.LoadControl("usercontrol1.ascx")

            Dim myUC As usercontrol1 = CType(myControl, usercontrol1)

            myUC.Name = "Patrick"

            Page.Controls.Add(myControl)

        End If



        Trace.Write("webform1", "Exiting webform1_Load")

    End Sub



===========  WEBFORM ===========



<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.vb"

Inherits="test.WebForm1"%>

<%@ Register TagPrefix="UC" TagName="myUCs" src="usercontrol1.ascx" %>



<html>

  <head>

    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">

    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">

  </head>

  <body>



    <form id="WebForm1" method="post" runat="server">



    Text Before the UserControl<br><p>



    <UC:myUCs id="myUC1" runat="Server" />



    Text After the UserControl



    </form>



  </body>

</html>



===========  USERCONTROL CODEBEHIND ===========



Private _name As String

    Public Property Name() As String

        Get

            Return _name

        End Get



        Set

            _name = value

        End Set

    End Property



    Protected Sub usercontrol1_Load(ByVal Sender As System.Object, ByVal e

As System.EventArgs)

        Trace.Write("usercontrol1", "Entering usercontrol1_Load with Name

property = " & Name)



        If Not IsPostback Then   ' Evals true first time browser hits the

page



            Response.Write("Ah, yes! My name is " & Name & "<br><p>")



        End If



        Trace.Write("webform1", "Exiting usercontrol1_Load")

    End Sub



===========  HOW THE PAGE RENDERS: ===========



This page will tell you my name...maybe:



Ah, yes! My name is





Ah, yes! My name is Patrick





Text Before the UserControl





This is some text from the usercontrol





Text After the UserControl

This is some text from the usercontrol





===========  QUESTIONS / OBSERVATIONS ===========



1. I tried a number of ways to set the Name property from the WebForm's

codebehind. The one that, at least from the VS.NET IntelliSense seemed like

it would work, was to declare an instance of the UserControl's codebehind

class (usercontrol1) and then simply set a Public variable strName (at the

time, not even a Property) on the codebehind:



    e.g., Dim myUserControl As usercontrol1

            myUserControl.strName



I got no compile errors with this syntax, but I would get a runtime

"Attempting to dereference a null object reference" error. Adding the

Trace.Write statements made me realize that the page leaves the Load event

prior to loading any UserControls. So there was no usercontrol1 class

instantiated on the server at the time I tried to set strName (I think I'm

right on that).



Does anyone have any comments on this? Am I on the right track? Does this

mean that the only (or best?) way to set a UserControl variable/property

value if it is needed in the UserControl's Load event is to use

Page.LoadControl and Page.Controls.Add?



2.  If my conclusions from #1 are correct, how do I avoid having the page

load and display the UserControl twice? I tried removing the @Register

directive but then it threw a predictable error that it couldn't identify

the tagprefix and tagname I was using to consume the UserControl in the

WebForm. So I tried removing <UC:myUCs id="myUC1" runat="Server" />

altogether but then this resulted in the text from the UserControl ("This is

some text from the usercontrol") displaying *prior* to the text from the

WebForm ("Text Before the UserControl"). Thus, how do I load a UserControl

from my WebForm's Load event and set properties AND have the usercontrol

show up in the right spot on the rendered page, and only when I want it to

(i.e., not twice).



3. This is a related question: What would the syntax be for accessing a

property of the WebForm called, for example, "Address" from the UserControl?

(E.g,: something like Parent.Page.Address?) What would be the syntax for

accessing the "Address" property of a UserControl whose ID is, for example,

"UC3" from another UserControl consumed by the same WebForm? (E.g.,

something like Parent.Page.Controls.FindControl("UC3").Address?



=======================



I hope this stimulates some good discussion on these matters. Of course, an

authoritative voice chiming in with comments and definitive answers would

also be nice ; - ).



Thanks to all who have read this far,



Patrick






  Return to Index