Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Microsoft Docs on User Controls


Message #1 by "J Donahue" <jdonahue@f...> on Thu, 13 Jun 2002 16:40:16
Hello.  I'm trying to access the variables on a user control in the aspx 
page.  I looked up User Controls in Visual Studios help and found the 
information I want but it doesn't make sense (at least not to me). 
Per Microsoft doc, one method of accessing usercontrols is as follows: 

MS Docs---------- 
At the top of the page, before the <HTML> tag, add a directive that 
registers the control so it will be recognized when the page is 
processed. You use the directive to associate a name and a namespace with 
the Web user control by specifying TagPrefix, TagName, and Src location 
values. For example: 
<%@ Register TagPrefix="myNameSpace" TagName="myUserControl" 
Src="userControl1.ascx" %> 

In the <BODY> portion of the file, create a tag for your control where 
you want the control to appear. Use the TagPrefix and TagName you 
registered in Step 2. Give your control an ID and set the attribute 
runat=server, as in the following example: 
<myNameSpace:myUserControl ID="mnuMain" runat="server" /> 

In the CODE-BEHIND Declarations area of the aspx page, add a line to 
declare the user control. For example, for a user control of type 
WebUserControl1 whose ID is mnuMain: 
' Visual Basic 
Public Class MyPage 
  Inherits System.Web.UI.Page 
  Protected mnuMain As WebUserControl1 
End of MS DOCS---------------- 

Where the heck did "WebUserControl1" come from?  It causes an error and I 
can't figure out the correct item to resolve it. 

All I'm trying to do is access simply data types in an ascx page from the 
aspx page.  I do the following in the ascx page: 
<%@ Register TagPrefix="expo" TagName="LabelBox" Src="ReqBaseInfo.ascx" %
> 
<expo:LabelBox runat="server" id="thePagelet" /> 'to display the ascx 
page where I want it. 

In the ascx page, I have the following (as per docs I found): 
Public Property MYReqAmount() As String 
  Get 
      Return ReqAmount.Text 
  End Get 
  Set(ByVal Value As String) 
      ReqAmount.Text = Value 
  End Set 
End Property 

In the aspx code behind page, I want to set the value of an ascx textbox 
ReqAmount by doing the following: 
thePagelet.MYReqAmount = "100000" 
Problem is I don't know how to declare "thePagelet" in the aspx code-
behind page. 
Protected thePagelet As ????????? 

Thanks!! 
Message #2 by "J Donahue" <jdonahue@f...> on Thu, 13 Jun 2002 17:04:21
Duh!  I was given the answer and thought I'd pass it along, as follows:

You're getting hung up on something simple because you are looking for a 
complex answer! 
When you use "as" it means you are about to specify a class of which the 
previous identifier will be an instance. 
Protected mnuMain As Integer 
Protected mnuMain As String 
Protected mnuMain As WebUserControl1 
etc... 
You have to tell it what mnuMain IS.  In this case the class for their 
user control in the example was WebUserControl1.  So for yours to work, 
you have to put in your class name there

since my usercontrol class was "myusercontrol", I simply had to declare 
it as:
Protected thePagelet As mysercontrol
Sheesh!


> Hello.  I'm trying to access the variables on a user control in the 
aspx 
p> age.  I looked up User Controls in Visual Studios help and found the 
i> nformation I want but it doesn't make sense (at least not to me). 
P> er Microsoft doc, one method of accessing usercontrols is as follows: 

> MS Docs---------- 
A> t the top of the page, before the <HTML> tag, add a directive that 
r> egisters the control so it will be recognized when the page is 
p> rocessed. You use the directive to associate a name and a namespace 
with 
t> he Web user control by specifying TagPrefix, TagName, and Src location 
v> alues. For example: 
<> %@ Register TagPrefix="myNameSpace" TagName="myUserControl" 
S> rc="userControl1.ascx" %> 

> In the <BODY> portion of the file, create a tag for your control where 
y> ou want the control to appear. Use the TagPrefix and TagName you 
r> egistered in Step 2. Give your control an ID and set the attribute 
r> unat=server, as in the following example: 
<> myNameSpace:myUserControl ID="mnuMain" runat="server" /> 

> In the CODE-BEHIND Declarations area of the aspx page, add a line to 
d> eclare the user control. For example, for a user control of type 
W> ebUserControl1 whose ID is mnuMain: 
'>  Visual Basic 
P> ublic Class MyPage 
 >  Inherits System.Web.UI.Page 
 >  Protected mnuMain As WebUserControl1 
E> nd of MS DOCS---------------- 

> Where the heck did "WebUserControl1" come from?  It causes an error and 
I 
c> an't figure out the correct item to resolve it. 

> All I'm trying to do is access simply data types in an ascx page from 
the 
a> spx page.  I do the following in the ascx page: 
<> %@ Register TagPrefix="expo" TagName="LabelBox" Src="ReqBaseInfo.ascx" 
%
>>  
<> expo:LabelBox runat="server" id="thePagelet" /> 'to display the ascx 
p> age where I want it. 

> In the ascx page, I have the following (as per docs I found): 
P> ublic Property MYReqAmount() As String 
 >  Get 
 >      Return ReqAmount.Text 
 >  End Get 
 >  Set(ByVal Value As String) 
 >      ReqAmount.Text = Value 
 >  End Set 
E> nd Property 

> In the aspx code behind page, I want to set the value of an ascx 
textbox 
R> eqAmount by doing the following: 
t> hePagelet.MYReqAmount = "100000" 
P> roblem is I don't know how to declare "thePagelet" in the aspx code-
b> ehind page. 
P> rotected thePagelet As ????????? 

> Thanks!! 
Message #3 by Imar Spaanjaars <Imar@S...> on Thu, 13 Jun 2002 18:12:31 +0200
Well, as far as I can see, your "thePagelet" is Microsoft's "WebUserControl1"


When you create your user control in the ASPX page, you have to give it an 
ID attribute. Then, in your code behind, declare a variable of type 
YourUserControl. Name the variable the same as the ID in the UserControl.

So your UserControl in the ASPX page looks like this:

<YourTagPrefix:UserControlName id="YourID" 
runat="server"></YourTagPrefix:UserControlName>

Then, in your code behind, declare the following:

   Protected YourID As UserControlName

Now you can programmatically access public stuff in the UserControl YourID


HtH

Imar




At 04:40 PM 6/13/2002 +0000, you wrote:
>Hello.  I'm trying to access the variables on a user control in the aspx
>page.  I looked up User Controls in Visual Studios help and found the
>information I want but it doesn't make sense (at least not to me).
>Per Microsoft doc, one method of accessing usercontrols is as follows:
>
>MS Docs----------
>At the top of the page, before the <HTML> tag, add a directive that
>registers the control so it will be recognized when the page is
>processed. You use the directive to associate a name and a namespace with
>the Web user control by specifying TagPrefix, TagName, and Src location
>values. For example:
><%@ Register TagPrefix="myNameSpace" TagName="myUserControl"
>Src="userControl1.ascx" %>
>
>In the <BODY> portion of the file, create a tag for your control where
>you want the control to appear. Use the TagPrefix and TagName you
>registered in Step 2. Give your control an ID and set the attribute
>runat=server, as in the following example:
><myNameSpace:myUserControl ID="mnuMain" runat="server" />
>
>In the CODE-BEHIND Declarations area of the aspx page, add a line to
>declare the user control. For example, for a user control of type
>WebUserControl1 whose ID is mnuMain:
>' Visual Basic
>Public Class MyPage
>   Inherits System.Web.UI.Page
>   Protected mnuMain As WebUserControl1
>End of MS DOCS----------------
>
>Where the heck did "WebUserControl1" come from?  It causes an error and I
>can't figure out the correct item to resolve it.
>
>All I'm trying to do is access simply data types in an ascx page from the
>aspx page.  I do the following in the ascx page:
><%@ Register TagPrefix="expo" TagName="LabelBox" Src="ReqBaseInfo.ascx" %
> >
><expo:LabelBox runat="server" id="thePagelet" /> 'to display the ascx
>page where I want it.
>
>In the ascx page, I have the following (as per docs I found):
>Public Property MYReqAmount() As String
>   Get
>       Return ReqAmount.Text
>   End Get
>   Set(ByVal Value As String)
>       ReqAmount.Text = Value
>   End Set
>End Property
>
>In the aspx code behind page, I want to set the value of an ascx textbox
>ReqAmount by doing the following:
>thePagelet.MYReqAmount = "100000"
>Problem is I don't know how to declare "thePagelet" in the aspx code-
>behind page.
>Protected thePagelet As ?????????
>
>Thanks!!



  Return to Index