2017-10-12 72 views
0

這個函數在頁面後面的代碼中完美地工作,但我需要全局訪問這個函數。所以我把它移動到app_code文件夾。現在,我如何以我的新app_code File函數找到它的方式聲明我的標籤ID,我有點失落。App_Code函數問題:在App_Code函數中未找到/聲明標籤ID?

my error message =錯誤35'Label1'未被聲明。由於其保護級別,它可能無法訪問。 注:我不不是我的代碼得到這個錯誤隱藏文件

日Thnx

命名空間BranchHours 公共類branchHours

' 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> 
+0

而不是在方法中使用Label1,您應該創建一個字符串變量並從方法返回它 –

回答

1

通過回答評論Chetan Ranpariya
代替在會議中使用Label1你應該創建一個字符串變量並將其從方法返回 - Chetan Ranpariya