HighTechTalks DotNet Forums  

Public variable not seen by all controls on a Page

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss Public variable not seen by all controls on a Page in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
John Kotuby
 
Posts: n/a

Default Public variable not seen by all controls on a Page - 12-12-2007 , 12:55 PM






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



Reply With Quote
  #2  
Old   
Peter Bromberg [C# MVP]
 
Posts: n/a

Default RE: Public variable not seen by all controls on a Page - 12-12-2007 , 01:16 PM






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:

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




Reply With Quote
  #3  
Old   
John Kotuby
 
Posts: n/a

Default Re: Public variable not seen by all controls on a Page - 12-12-2007 , 04:34 PM



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" %>
<%@ MasterType VirtualPath="~/SearchPage.master" %>
<%@ Register Src="../Controls/User/PCSavedSearches.ascx"
TagName="PCSavedSearches"
TagPrefix="uc2" %>
<%@ Register Src="../Controls/User/CreateQuickSearchBar.ascx"
TagName="CreateQuickSearchBar"
TagPrefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Content1" Runat="Server">

<% If Not quickadd = "Y" Then%>

<uc1:CreateQuickSearchBar ID="CreateQuickSearchBar1" runat="server" />

<% End If%>

<uc2:PCSavedSearches ID="PCSavedSearches1" runat="server" />
<input type="hidden" name="txtDeckName" id="txtDeckName"
value="<%=strDeckName %>" />
</asp:Content>

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

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

<% If Not Controls_User_PCSavedSearches.QuickAdd = "Y" Then%>

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]" <pbromberg (AT) yahoo (DOT) NoSpamMaam.com> wrote

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






Reply With Quote
  #4  
Old   
Scott Roberts
 
Posts: n/a

Default Re: Public variable not seen by all controls on a Page - 12-12-2007 , 05:08 PM



"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:

<% If Not PCSavedSearches1.quickAdd = "Y" Then %>

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.



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" %
%@ MasterType VirtualPath="~/SearchPage.master" %
%@ Register Src="../Controls/User/PCSavedSearches.ascx"
TagName="PCSavedSearches"
TagPrefix="uc2" %
%@ Register Src="../Controls/User/CreateQuickSearchBar.ascx"
TagName="CreateQuickSearchBar"
TagPrefix="uc1" %

asp:Content ID="Content1" ContentPlaceHolderID="Content1" Runat="Server"

% If Not quickadd = "Y" Then%

uc1:CreateQuickSearchBar ID="CreateQuickSearchBar1" runat="server" /

% End If%

uc2:PCSavedSearches ID="PCSavedSearches1" runat="server" /
input type="hidden" name="txtDeckName" id="txtDeckName"
value="<%=strDeckName %>" /
/asp:Content

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

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

% If Not Controls_User_PCSavedSearches.QuickAdd = "Y" Then%

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]" <pbromberg (AT) yahoo (DOT) NoSpamMaam.com> wrote in
message news:8B0E54D8-7108-4A87-87B4-1446A6E496C3 (AT) microsoft (DOT) 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...







Reply With Quote
  #5  
Old   
John Kotuby
 
Posts: n/a

Default Re: Public variable not seen by all controls on a Page - 12-12-2007 , 05:30 PM



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" <sroberts (AT) no (DOT) spam.here-webworks-software.com> wrote in
message news:uoerKuQPIHA.5980 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
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:

% If Not PCSavedSearches1.quickAdd = "Y" Then %

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" %
%@ MasterType VirtualPath="~/SearchPage.master" %
%@ Register Src="../Controls/User/PCSavedSearches.ascx"
TagName="PCSavedSearches"
TagPrefix="uc2" %
%@ Register Src="../Controls/User/CreateQuickSearchBar.ascx"
TagName="CreateQuickSearchBar"
TagPrefix="uc1" %

asp:Content ID="Content1" ContentPlaceHolderID="Content1"
Runat="Server"

% If Not quickadd = "Y" Then%

uc1:CreateQuickSearchBar ID="CreateQuickSearchBar1" runat="server" /

% End If%

uc2:PCSavedSearches ID="PCSavedSearches1" runat="server" /
input type="hidden" name="txtDeckName" id="txtDeckName"
value="<%=strDeckName %>" /
/asp:Content

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

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

% If Not Controls_User_PCSavedSearches.QuickAdd = "Y" Then%

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]" <pbromberg (AT) yahoo (DOT) NoSpamMaam.com> wrote in
message news:8B0E54D8-7108-4A87-87B4-1446A6E496C3 (AT) microsoft (DOT) 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...









Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.