views:

2611

answers:

3

I have a class isSearching with a single boolean property in a 'functions' file in my webapp. On my search page, I have a variable oSearchHandler declared as a Public Shared variable. How can I access the contents of oSearchHandler on other pages in my webapp?

Code with Session....

'search.aspx
Public Function oSearchString(ByVal oTextBoxName As String) As String
    For Each oKey As String In Request.Form.AllKeys
        If oKey.Contains(oTextBoxName) Then
            Session.Add("searching", True)
            Session.Add("search-term", Request.Form(oKey))
            Return Request.Form(oKey)
        End If
    Next
    Return ""
End Function

'theMaster.master
<%
If Session("searching") Then
%><ul style="float: right;">
    <li>
        <div class="gsSearch">
            <asp:TextBox ID="searchbox" runat="server"></asp:TextBox>
        </div>
    </li>
    <li>
        <div class="gsSearch">
            <asp:Button ID="searchbutton" runat="server" Text="search" UseSubmitBehavior="true" PostBackUrl="search.aspx" CssClass="searchBtn" />
        </div>
    </li>
</ul>
<%
End If
%>

I think that the session will work just fine.

+2  A: 

If you're talking about accessing these variables between page interactions, you need to bear in mind that the state is discarded between pages.

Instead, you need to store this data in Session State.

If it's not across page interactions, but simply accessing the data from other parts of the code, the key is that the ASP.NET page becomes a class and your public shared variable, a static property of that class.

So, you'd access it from elsewhere using PageName.oSearchHandler

[EDIT] Can you give us some more information about what oSearchHandler is and how you're intending using it? We can probably offer a more considered recommendation, then.

Steve Morgan
Do you have a link handy that could get me started? I have not utilized Session States with ASP.NET before. Thanks for your input!
Anders
If it's a static variable, will it the information be shared between multiple sessions? StackOverflow should have an area to post source code files :) that would be helpful.
stephenbayer
@stephenbayer - good point - the scope of the static is the App Domain, not the thread, so it's potentially dangerous.
Steve Morgan
It is not to be shared between sessions.
Anders
yeah, there's really a billion ways to access variables across pages, it really depends on your implementation and what you need to do or use it for. The variable naming is important as well.. Given that name, I would assume that oShearchHandler was a delegate that returned a generic object.
stephenbayer
Yes you are correct, it is a Class with a single Boolean. I think I am just going to try to see if the Session() will do what I need. Thanks again.
Anders
+2  A: 

If you want it accessible from multiple pages you should pull it off that individual page class and put it in a more globally accessable place such as the Application collection. Given the naming of the variable, is 0SearchHandler a delegate? I'm not as familiar with VB.NET as much or the terminology.

Update: Steve Morgan mentioned using the Session collection, when "static" or "shared" was mentioned, i was thinking more globally. Depending on how your using the variable you can use the "Application" if it will be shared between users and sessions, or the "session" if it will be used by one user in one session. In VB.NET they are both easy to use:

Session("yourKey") = YourObjectYouWantToSave
Application("yourKey") = YourObjectYouWantToSave

Very simple stuff.

'search.aspx
Public Function oSearchString(ByVal oTextBoxName As String) As String
    For Each oKey As String In Request.Form.AllKeys
        If oKey.Contains(oTextBoxName) Then
            Session("searching") = True
            Session("search-term") =  Request.Form(oKey)
            Return Request.Form(oKey)
        End If
    Next
    Return ""
End Function
' theMaster.master.vb
In PageLoad Method:
...
Dim bSearching as Boolean
bSearching = IIf(Session("searching") is Nothing, False, Session("searching") )

ulSearch.visible = bSearching
...

'theMaster.master
<ul style="float: right;" runat="server" id="ulSearch">
    <li>
        <div class="gsSearch">
            <asp:TextBox ID="searchbox" runat="server"></asp:TextBox>
        </div>
    </li>
    <li>
        <div class="gsSearch">
            <asp:Button ID="searchbutton" runat="server" Text="search" UseSubmitBehavior="true" PostBackUrl="search.aspx" CssClass="searchBtn" />
        </div>
    </li>
</ul>

Ok, that is some extra code but I think you would have less problems with it. Plus my VB is a bit rusty. Actually, If the master page is the page you will be using it on, I would put the variable as a public property on that masterpage. You can access the pages master page with this.Master (at least in C#, I think it's Me.Master in VB.NET).

stephenbayer
A: 

Nice article. It really gave us a lot of information. We were trying to get information on some subject which is related to this to an extent for last couple of days and this article really helped us. Good job !

eXtendCode

Alok Kumar