MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Public variable not seen by all controls on a Page

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> ASPNet
Author Message
John Kotuby



Joined: 08 Aug 2007
Posts: 5

PostPosted: Wed Dec 12, 2007 5:55 pm    Post subject: Public variable not seen by all controls on a Page Reply with quote

Hi all,

In ASP.NET 2.0 I have some pages that are composed of a Master Page and
several User controls. In one of the pages I declare a Public variable
within the code-behind of a User Control on the page.

I populate the variable during the Page_Load event of the User Control.

When I reference the variable in another part of the page, specifically in
Page_Load of the main ASPX or even Page_PreRender VS tells me the variable
is not declared.

Is there a place in the page where I can declare a variable (maybe using
something other than Public for the declaration?) so that it can be seen and
referenced by all members of the page, but Only within the page itself?

Thanks...

Archived from group: microsoft>public>dotnet>framework>aspnet
Back to top
View user's profile Send private message
Peter Bromberg [C# MVP]



Joined: 31 Oct 2007
Posts: 22

PostPosted: Wed Dec 12, 2007 3:16 pm    Post subject: RE: Public variable not seen by all controls on a Page Reply with quote

You don't specify in your post how and with what code you are declaring this
variable. If your field is a public field in the UserControl or a public
property, then from anywhere in the Page you should be able to use
FindControl method to get a reference to your UserControl and access the
property and its value.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


"John Kotuby" wrote:

> Hi all,
>
> In ASP.NET 2.0 I have some pages that are composed of a Master Page and
> several User controls. In one of the pages I declare a Public variable
> within the code-behind of a User Control on the page.
>
> I populate the variable during the Page_Load event of the User Control.
>
> When I reference the variable in another part of the page, specifically in
> Page_Load of the main ASPX or even Page_PreRender VS tells me the variable
> is not declared.
>
> Is there a place in the page where I can declare a variable (maybe using
> something other than Public for the declaration?) so that it can be seen and
> referenced by all members of the page, but Only within the page itself?
>
> Thanks...
>
>
>
Back to top
View user's profile Send private message
John Kotuby



Joined: 08 Aug 2007
Posts: 5

PostPosted: Wed Dec 12, 2007 9:34 pm    Post subject: Re: Public variable not seen by all controls on a Page Reply with quote

Sorry for the obvious lack of details. I am using VB.NET and declaring a
simple string variable named "quickAdd".
------------------------------------
Partial Class Controls_User_PCSavedSearches
Inherits System.Web.UI.UserControl
Public quickAdd
End Sub
----------------------------------------

The User control that belongs to the code-behind class is added to the main
ASPX page.
------------------------------------------
<%@ Page Language="VB" MasterPageFile="~/SearchPage.master"
AutoEventWireup="false" EnableSessionState="True"
CodeFile="chooseSearch.aspx.vb" Inherits="Search_chooseSearch" title="Saved
Search Criteria" %>

<%@ Register Src="../Controls/User/PCSavedSearches.ascx"
TagName="PCSavedSearches"
TagPrefix="uc2" %>
<%@ Register Src="../Controls/User/CreateQuickSearchBar.ascx"
TagName="CreateQuickSearchBar"
TagPrefix="uc1" %>










value="" />


--------------------------------------------

I make reference to the variable and of course the compiler tells me it is
not declared, even though the Control is registered and is part of the ASPX
page. I guess I was wrong in thinking that if a simple string variable is
declared Public, even within a class, that it might be viewable outside the
class. I am wondering if I create a Public Property named QuickAdd and feed
it the contents of the variable quickAdd that I can do something like...



But I would most likely need to instantiate the class before referring to
it.

Sorry to bother you all with such trivial stuff.

"Peter Bromberg [C# MVP]" wrote in message @microsoft.com...
> You don't specify in your post how and with what code you are declaring
> this
> variable. If your field is a public field in the UserControl or a public
> property, then from anywhere in the Page you should be able to use
> FindControl method to get a reference to your UserControl and access the
> property and its value.
> -- Peter
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> MetaFinder: http://www.blogmetafinder.com
>
>
> "John Kotuby" wrote:
>
>> Hi all,
>>
>> In ASP.NET 2.0 I have some pages that are composed of a Master Page and
>> several User controls. In one of the pages I declare a Public variable
>> within the code-behind of a User Control on the page.
>>
>> I populate the variable during the Page_Load event of the User Control.
>>
>> When I reference the variable in another part of the page, specifically
>> in
>> Page_Load of the main ASPX or even Page_PreRender VS tells me the
>> variable
>> is not declared.
>>
>> Is there a place in the page where I can declare a variable (maybe using
>> something other than Public for the declaration?) so that it can be seen
>> and
>> referenced by all members of the page, but Only within the page itself?
>>
>> Thanks...
>>
>>
>>
Back to top
View user's profile Send private message
Scott Roberts



Joined: 15 Nov 2007
Posts: 23

PostPosted: Wed Dec 12, 2007 9:08 pm    Post subject: Re: Public variable not seen by all controls on a Page Reply with quote

"quickAdd" is public, but it's not "Global". It's still a property of the
User Control, so you must reference it through the User Control. Try this:



Also note that you have the property declared as "quickAdd" but you are
referencing "quickadd". I'm not familiar with VB, but in C# property names
are case-sensitive.



> Sorry for the obvious lack of details. I am using VB.NET and declaring a
> simple string variable named "quickAdd".
> ------------------------------------
> Partial Class Controls_User_PCSavedSearches
> Inherits System.Web.UI.UserControl
> Public quickAdd
> End Sub
> ----------------------------------------
>
> The User control that belongs to the code-behind class is added to the
> main ASPX page.
> ------------------------------------------
> <%@ Page Language="VB" MasterPageFile="~/SearchPage.master"
> AutoEventWireup="false" EnableSessionState="True"
> CodeFile="chooseSearch.aspx.vb" Inherits="Search_chooseSearch"
> title="Saved Search Criteria" %>
>
> <%@ Register Src="../Controls/User/PCSavedSearches.ascx"
> TagName="PCSavedSearches"
> TagPrefix="uc2" %>
> <%@ Register Src="../Controls/User/CreateQuickSearchBar.ascx"
> TagName="CreateQuickSearchBar"
> TagPrefix="uc1" %>
>
>
>
>
>
>
>
>
>
>
> > value="" />
>
>
> --------------------------------------------
>
> I make reference to the variable and of course the compiler tells me it is
> not declared, even though the Control is registered and is part of the
> ASPX page. I guess I was wrong in thinking that if a simple string
> variable is declared Public, even within a class, that it might be
> viewable outside the class. I am wondering if I create a Public Property
> named QuickAdd and feed it the contents of the variable quickAdd that I
> can do something like...
>
>
>
> But I would most likely need to instantiate the class before referring to
> it.
>
> Sorry to bother you all with such trivial stuff.
>
> "Peter Bromberg [C# MVP]" wrote in
> message @microsoft.com...
>> You don't specify in your post how and with what code you are declaring
>> this
>> variable. If your field is a public field in the UserControl or a public
>> property, then from anywhere in the Page you should be able to use
>> FindControl method to get a reference to your UserControl and access the
>> property and its value.
>> -- Peter
>> Site: http://www.eggheadcafe.com
>> UnBlog: http://petesbloggerama.blogspot.com
>> MetaFinder: http://www.blogmetafinder.com
>>
>>
>> "John Kotuby" wrote:
>>
>>> Hi all,
>>>
>>> In ASP.NET 2.0 I have some pages that are composed of a Master Page and
>>> several User controls. In one of the pages I declare a Public variable
>>> within the code-behind of a User Control on the page.
>>>
>>> I populate the variable during the Page_Load event of the User Control.
>>>
>>> When I reference the variable in another part of the page, specifically
>>> in
>>> Page_Load of the main ASPX or even Page_PreRender VS tells me the
>>> variable
>>> is not declared.
>>>
>>> Is there a place in the page where I can declare a variable (maybe using
>>> something other than Public for the declaration?) so that it can be seen
>>> and
>>> referenced by all members of the page, but Only within the page itself?
>>>
>>> Thanks...
>>>
>>>
>>>
>
>
Back to top
View user's profile Send private message
John Kotuby



Joined: 08 Aug 2007
Posts: 5

PostPosted: Wed Dec 12, 2007 10:30 pm    Post subject: Re: Public variable not seen by all controls on a Page Reply with quote

Thanks Scott,
You have been really helpful and thanks for not torching me on a such a
fundamental question.
VB allows for case insensitivity, but it is always good practice to practice
using proper case, as I am sometimes forced into writing Javascript routines
to attain a certain result. I am getting the sense that VB.NET will soon be
a thing of the past...even with assurances for Microsoft that it will
continue support.

"Scott Roberts" wrote in
message @TK2MSFTNGP04.phx.gbl...
> "quickAdd" is public, but it's not "Global". It's still a property of the
> User Control, so you must reference it through the User Control. Try this:
>
>
>
> Also note that you have the property declared as "quickAdd" but you are
> referencing "quickadd". I'm not familiar with VB, but in C# property names
> are case-sensitive.
>
>
>
>> Sorry for the obvious lack of details. I am using VB.NET and declaring a
>> simple string variable named "quickAdd".
>> ------------------------------------
>> Partial Class Controls_User_PCSavedSearches
>> Inherits System.Web.UI.UserControl
>> Public quickAdd
>> End Sub
>> ----------------------------------------
>>
>> The User control that belongs to the code-behind class is added to the
>> main ASPX page.
>> ------------------------------------------
>> <%@ Page Language="VB" MasterPageFile="~/SearchPage.master"
>> AutoEventWireup="false" EnableSessionState="True"
>> CodeFile="chooseSearch.aspx.vb" Inherits="Search_chooseSearch"
>> title="Saved Search Criteria" %>
>>
>> <%@ Register Src="../Controls/User/PCSavedSearches.ascx"
>> TagName="PCSavedSearches"
>> TagPrefix="uc2" %>
>> <%@ Register Src="../Controls/User/CreateQuickSearchBar.ascx"
>> TagName="CreateQuickSearchBar"
>> TagPrefix="uc1" %>
>>
>> >> Runat="Server">
>>
>>
>>
>>
>>
>>
>>
>>
>> >> value="" />
>>
>>
>> --------------------------------------------
>>
>> I make reference to the variable and of course the compiler tells me it
>> is not declared, even though the Control is registered and is part of the
>> ASPX page. I guess I was wrong in thinking that if a simple string
>> variable is declared Public, even within a class, that it might be
>> viewable outside the class. I am wondering if I create a Public Property
>> named QuickAdd and feed it the contents of the variable quickAdd that I
>> can do something like...
>>
>>
>>
>> But I would most likely need to instantiate the class before referring to
>> it.
>>
>> Sorry to bother you all with such trivial stuff.
>>
>> "Peter Bromberg [C# MVP]" wrote in
>> message @microsoft.com...
>>> You don't specify in your post how and with what code you are declaring
>>> this
>>> variable. If your field is a public field in the UserControl or a public
>>> property, then from anywhere in the Page you should be able to use
>>> FindControl method to get a reference to your UserControl and access the
>>> property and its value.
>>> -- Peter
>>> Site: http://www.eggheadcafe.com
>>> UnBlog: http://petesbloggerama.blogspot.com
>>> MetaFinder: http://www.blogmetafinder.com
>>>
>>>
>>> "John Kotuby" wrote:
>>>
>>>> Hi all,
>>>>
>>>> In ASP.NET 2.0 I have some pages that are composed of a Master Page and
>>>> several User controls. In one of the pages I declare a Public variable
>>>> within the code-behind of a User Control on the page.
>>>>
>>>> I populate the variable during the Page_Load event of the User Control.
>>>>
>>>> When I reference the variable in another part of the page, specifically
>>>> in
>>>> Page_Load of the main ASPX or even Page_PreRender VS tells me the
>>>> variable
>>>> is not declared.
>>>>
>>>> Is there a place in the page where I can declare a variable (maybe
>>>> using
>>>> something other than Public for the declaration?) so that it can be
>>>> seen and
>>>> referenced by all members of the page, but Only within the page itself?
>>>>
>>>> Thanks...
>>>>
>>>>
>>>>
>>
>>
>

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
[Ann] Limited Public Beta - XHTML 1.0 Strict Web Controls We are pleased to announce we are accepting applications to participate in our limited public beta of The controls are a complete replacement for the namespace. These controls produce 100% XHTML Strict 1.0 compl

Composite Controls and Me.Page.IsPostback Problem Hi all, In my Composite Control's Sub New Function I am unable to check if Me.Page is Nothing (so i get a Do I have to pass the Page Object to my composite control to get access to IsPostBack? Thanks. -- Lucas

Maintaining session and auth. ticket from page to page redir Hi, I'm hoping that someone can clear some of my confusion as I cannot seem to find a complete answer on the subject. I'm using VS.NET 2003 and creating an ASP.NET mobile application. I'm using Forms to authenticate the user. Now, I'm assum

Accessing Properties of Custom Controls child Controls I am using a Custom Control on a page which renders a button control if required. I need to access the child button control's properties (i.e. UniqueID) on the page but cannot get them to come through on first load of the page, only on postback. Can anyon

howto access controls in User Controls Hi, How can I access HTML controls in User Controls with JScript? Thanks a lot, Derda
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> ASPNet All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group