App_Code 功能问题:App_Code 功能中的标签 ID 不是 found/declared?
App_Code Function Issues: Label ID not being found/declared in App_Code Function?
此功能在我的代码隐藏页面中完美运行,但我需要全局访问此功能。所以我将其移至 app_code 文件夹。现在,我对如何以我的新 app_code 文件函数找到它的方式声明我的标签 ID 有点迷茫。
我的错误消息 = 错误 35 'Label1' 未声明。由于其保护级别,它可能无法访问。
注意:我的代码隐藏文件
中没有出现此错误
谢谢
命名空间 BranchHours
Public Class 营业时间
' Public Shared Property Label1 As Object
Public Shared Function MyFunc(ByVal branchCode As String) As String
Dim URLString As String = "url/branchesTesting.xml"
Dim xmlDoc As XDocument = XDocument.Load(URLString)
' Dim root As XmlNode = xmlDoc.DocumentElement
' Dim hoursList As XmlNodeList = root.SelectNodes("/BranchesInfo/BranchInfo[BranchId='db']/BranchHours/Hours")
Dim x As Integer = 0 'looping variable
Dim branchid = xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "']/BranchHours/Hours")
Dim branchname = xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "'/Name]/BranchHours/Hours")
Dim Items = From BranchHours In xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "']/BranchHours/Hours") _
Select DayOfWeek = BranchHours.Element("DayOfWeek").Value, Open = BranchHours.Element("Open").Value, _
Close = BranchHours.Element("Close").Value
Label1.Text = ""
For Each Hours In Items
' If x = 0 Then
' Label1.Text = Label1.Text & "<div>DayOfWeek:" & Hours.DayOfWeek & "</div>" + vbLf
Label1.Text = Label1.Text & "<div>DayOfWeek:" & Hours.DayOfWeek & "</div>" + vbLf
Label1.Text = Label1.Text & "Open: " & Hours.Open + vbLf
Label1.Text = Label1.Text & "Close: " & Hours.Close + vbLf
' Else
' x = x + 1
' End If
Next
If Label1.Text = "" Then
Label1.Text = "No Results."
End If
Return Label1.Text
End Function
End Class
结束命名空间
Public Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Call BranchHours.branchHours.MyFunc("dr")
Label1.Text = BranchHours.branchHours.MyFunc("dr")
End Sub
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default3bh.aspx.vb" Inherits="Default3bh" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Label ID="Label1" runat="server" For="label1"></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="AdditionalContent" Runat="Server">
</asp:Content>
Chetan Ranpariya 在评论中回答
与其在方法中使用 Label1,不如从方法中创建一个字符串变量 return – Chetan Ranpariya
此功能在我的代码隐藏页面中完美运行,但我需要全局访问此功能。所以我将其移至 app_code 文件夹。现在,我对如何以我的新 app_code 文件函数找到它的方式声明我的标签 ID 有点迷茫。
我的错误消息 = 错误 35 'Label1' 未声明。由于其保护级别,它可能无法访问。 注意:我的代码隐藏文件
中没有出现此错误谢谢
命名空间 BranchHours Public Class 营业时间
' Public Shared Property Label1 As Object
Public Shared Function MyFunc(ByVal branchCode As String) As String
Dim URLString As String = "url/branchesTesting.xml"
Dim xmlDoc As XDocument = XDocument.Load(URLString)
' Dim root As XmlNode = xmlDoc.DocumentElement
' Dim hoursList As XmlNodeList = root.SelectNodes("/BranchesInfo/BranchInfo[BranchId='db']/BranchHours/Hours")
Dim x As Integer = 0 'looping variable
Dim branchid = xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "']/BranchHours/Hours")
Dim branchname = xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "'/Name]/BranchHours/Hours")
Dim Items = From BranchHours In xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "']/BranchHours/Hours") _
Select DayOfWeek = BranchHours.Element("DayOfWeek").Value, Open = BranchHours.Element("Open").Value, _
Close = BranchHours.Element("Close").Value
Label1.Text = ""
For Each Hours In Items
' If x = 0 Then
' Label1.Text = Label1.Text & "<div>DayOfWeek:" & Hours.DayOfWeek & "</div>" + vbLf
Label1.Text = Label1.Text & "<div>DayOfWeek:" & Hours.DayOfWeek & "</div>" + vbLf
Label1.Text = Label1.Text & "Open: " & Hours.Open + vbLf
Label1.Text = Label1.Text & "Close: " & Hours.Close + vbLf
' Else
' x = x + 1
' End If
Next
If Label1.Text = "" Then
Label1.Text = "No Results."
End If
Return Label1.Text
End Function
End Class
结束命名空间
Public Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Call BranchHours.branchHours.MyFunc("dr")
Label1.Text = BranchHours.branchHours.MyFunc("dr")
End Sub
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default3bh.aspx.vb" Inherits="Default3bh" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Label ID="Label1" runat="server" For="label1"></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="AdditionalContent" Runat="Server">
</asp:Content>
Chetan Ranpariya 在评论中回答
与其在方法中使用 Label1,不如从方法中创建一个字符串变量 return – Chetan Ranpariya