Public variable not seen by all controls on a Page

J

John Kotuby

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

John Kotuby

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

Scott Roberts

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

John Kotuby

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.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top