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?
我有一个类isSearching在我的webapp中的'functions'文件中有一个布尔属性。在我的搜索页面上,我将变量oSearchHandler声明为公共共享变量。如何在我的webapp中的其他页面*问oSearchHandler的内容?
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 个解决方案
#1
2
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.
如果它不是跨页面交互,而只是从代码的其他部分访问数据,关键是ASP.NET页面成为一个类和您的公共共享变量,该类的静态属性。
So, you'd access it from elsewhere using PageName.oSearchHandler
因此,您可以使用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.
[编辑]您能否提供一些有关oSearchHandler是什么以及您打算如何使用它的更多信息?那么我们可能会提供更多考虑的建议。
#2
2
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.
如果您希望从多个页面访问它,则应将其从单个页面类中拉出,并将其放在更易于访问全局的位置,例如Application集合。鉴于变量的命名,0SearchHandler是一个委托吗?我不熟悉VB.NET或术语。
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集合,当提到“静态”或“共享”时,我在全球范围内思考更多。根据您使用变量的方式,如果它将在用户和会话之间共享,则可以使用“应用程序”,如果一个用户在一个会话中使用“会话”,则可以使用“会话”。在VB.NET中,它们都很容易使用:
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).
好的,这是一些额外的代码,但我认为你会遇到更少的问题。再加上我的VB有点生疏了。实际上,如果母版页是您将使用它的页面,我会将该变量作为公共属性放在该母版页上。您可以使用this.Master访问页面母版页(至少在C#中,我认为它是VB.NET中的Me.Master)。
#1
2
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.
如果它不是跨页面交互,而只是从代码的其他部分访问数据,关键是ASP.NET页面成为一个类和您的公共共享变量,该类的静态属性。
So, you'd access it from elsewhere using PageName.oSearchHandler
因此,您可以使用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.
[编辑]您能否提供一些有关oSearchHandler是什么以及您打算如何使用它的更多信息?那么我们可能会提供更多考虑的建议。
#2
2
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.
如果您希望从多个页面访问它,则应将其从单个页面类中拉出,并将其放在更易于访问全局的位置,例如Application集合。鉴于变量的命名,0SearchHandler是一个委托吗?我不熟悉VB.NET或术语。
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集合,当提到“静态”或“共享”时,我在全球范围内思考更多。根据您使用变量的方式,如果它将在用户和会话之间共享,则可以使用“应用程序”,如果一个用户在一个会话中使用“会话”,则可以使用“会话”。在VB.NET中,它们都很容易使用:
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).
好的,这是一些额外的代码,但我认为你会遇到更少的问题。再加上我的VB有点生疏了。实际上,如果母版页是您将使用它的页面,我会将该变量作为公共属性放在该母版页上。您可以使用this.Master访问页面母版页(至少在C#中,我认为它是VB.NET中的Me.Master)。