Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Public variable not seen by all controls on a Page

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Kotuby - 12 Dec 2007 17:55 GMT
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...
Peter Bromberg [C# MVP] - 12 Dec 2007 18:16 GMT
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 

> Hi all,
>
[quoted text clipped - 13 lines]
>
> Thanks...
John Kotuby - 12 Dec 2007 21:34 GMT
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.

> You don't specify in your post how and with what code you are declaring
> this
[quoted text clipped - 27 lines]
>>
>> Thanks...
Scott Roberts - 12 Dec 2007 22:08 GMT
"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".
[quoted text clipped - 81 lines]
>>>
>>> Thanks...
John Kotuby - 12 Dec 2007 22:30 GMT
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.

> "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:
[quoted text clipped - 92 lines]
>>>>
>>>> Thanks...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.