0

这是我的问题:

我使用了 Javascript #Next 'on click' 函数来调用该Public Shared Sub NextPage()方法,并在“If-else”语句中呈现我的 bookviewer。

但是,我现在需要Public Shared Sub NextPage()调用Protected Sub goToRequestedPage(). 我知道我必须调用另一个Public Shared Sub,所以我需要在这里做一些事情来从goToRequestedPage()方法中检索。以下是goToRequestedPage()我希望“公共共享子”调用的方法(或至少执行其中的语句)。

AutoSaveCheckBox, Label1, 和Response, 都说明了以下错误:“如果没有类的显式实例,则无法从共享方法或共享成员初始化程序中引用类的实例成员。”

如您所见,除了允许用户转到下一页之外,我还希望弹出一个消息框,要求用户保存。用户可以转到下一页,但我怎样才能实现该goToRequestPage()方法,或者至少让我的msgbox进程编译?

//Here is the Javascript 'on click' function for my next button:

$("#Next").click(function () {
                //PageMethods.NextPage(gbpageno)
                $.ajax({
                    type: "POST",
                    url: "WebForm1.aspx/NextPage",
                    async: true,
                    data: "{'page':'" + gbPageNo + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function () { },
                    error: function () { }
                });

                if (splitType == "v") {
                    RenderBR('#CenterLeft', gbPageNo + 1);
                }
                else {
                    RenderBR('#CenterTop', gbPageNo + 1);
                }
            });

//Here is the Public Shared Sub NextPage() method:

<System.Web.Services.WebMethod()> Public Shared Sub NextPage(ByVal page As Integer, ByVal textboxData As String)
        goToRequestedPage()
End Sub

//这是我想从 Public Shared Sub //NextPage() 调用的 goToRequestedPage() 方法,或者至少检索其中的所有功能(最初,这是 //Protected Sub goToRequestedPage()):

Public Shared Sub goToRequestedPage()
        ' Declare Variables:
        ' Declare TransTableAdapter
        Dim TransAdapter As New EOL_DataSetTableAdapters.TransTableAdapter
        ' Declare previous page save confirmation
        Dim previousPageSaveConfirmation As MsgBoxResult
        ' Declare next page save confirmation
        Dim nextPageSaveConfirmation As MsgBoxResult
        ' Declare autosave checkbox status as Boolean value
        Dim autoSaveCheckBoxStatus As Boolean
        ' Declare page link as String 
        Dim pgLink As String

        ' If autosave checkbox is CHECKED...
        If AutoSaveCheckBox.Checked = True Then
            ' Save (Either Insert or Update) Transcription and go to next page
            TransAdapter.Insert(DataID:="NOT ASSIGNED", ID:=1, Book:="Book1", Page:="Page1", User:="User1", urlPDF:="url1", Transcription:="Transcription1", Transcription_Text:=TranscriptionTextbox.Text, isMaster:=0, isLocked:=0, Timestamp:="2004-11-15 11:37:10", CreateDate:="2004-11-15 11:37:10")
            ' Add the incremented page link back to the String
            pgLink = "http://br.mdsa.net/BookReader/index.html?id=ce425&item=1&format=Bitonal#page/" & gbCurrentPage & "/mode/1up"
            ' Declare Label 1 as The BookViewer on the Requested Page Number
            Label1.Text = "<object id=""BookViewer"" data=""" & pgLink & """ height=""500"" width=""1500""><embed src=""" & pgLink & """ width=""95%"" height=""100%""/>Error: Embedded data could not be displayed.</object>"
            ' Redirect to the Global Current Page Number
            Response.Redirect("~/WebForm1.aspx?page=" & gbCurrentPage)

            ' If the autosave checkbox is UNCHECKED....
        Else
            If AutoSaveCheckBox.Checked = False Then
                'Display Yes/No Save Confirmation
                MsgBox("Would you like to save before continuing to the next page?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")

                ' For Previous Case:
                If previousPageSaveConfirmation = MsgBoxResult.Yes Then
                    ' Decrement Global Current Page Number by one page if greater than page 1:
                    'If gbCurrentPage > 1 Then gbCurrentPage -= 1
                    ' Save (Either Insert or Update) Transcription
                    TransAdapter.Insert(DataID:="NOT ASSIGNED", ID:=1, Book:="Book1", Page:="Page1", User:="User1", urlPDF:="url1", Transcription:="Transcription1", Transcription_Text:=TranscriptionTextbox.Text, isMaster:=0, isLocked:=0, Timestamp:="2004-11-15 11:37:10", CreateDate:="2004-11-15 11:37:10")
                    ' Add the decremented page link back to the String
                    pgLink = "http://br.mdsa.net/BookReader/index.html?id=ce425&item=1&format=Bitonal#page/" & gbCurrentPage & "/mode/1up"
                    ' Declare Label 1
                    Label1.Text = "<object id=""BookViewer"" data=""" & pgLink & """ height=""500"" width=""1500""><embed src=""" & pgLink & """ width=""95%"" height=""100%""/>Error: Embedded data could not be displayed.</object>"
                    ' Redirect to the Global Current Page Number
                    Response.Redirect("~/WebForm1.aspx?page=" & gbCurrentPage)
                ElseIf previousPageSaveConfirmation = MsgBoxResult.No Then
                    ' Decrement Global Current Page Number by one page if greater than page 1:
                    'If gbCurrentPage > 1 Then gbCurrentPage -= 1
                    ' Go to Previous Page:
                    ' Add the decremented page link back to the String
                    pgLink = "http://br.mdsa.net/BookReader/index.html?id=ce425&item=1&format=Bitonal#page/" & gbCurrentPage & "/mode/1up"
                    ' Add the incremented page link back to the String
                    pgLink = "http://br.mdsa.net/BookReader/index.html?id=ce425&item=1&format=Bitonal#page/" & gbCurrentPage & "/mode/1up"
                    ' Declare Label 1
                    Label1.Text = "<object id=""BookViewer"" data=""" & pgLink & """ height=""500"" width=""1500""><embed src=""" & pgLink & """ width=""95%"" height=""100%""/>Error: Embedded data could not be displayed.</object>"
                    ' Redirect to the Global Current Page Number
                    Response.Redirect("~/WebForm1.aspx?page=" & gbCurrentPage)
                End If

                ' For Next Case:
                If nextPageSaveConfirmation = MsgBoxResult.Yes Then
                    ' Increment global current page number by one page
                    'gbCurrentPage += 1
                    ' Save (Either Insert or Update) Transcription and go to next page
                    TransAdapter.Insert(DataID:="NOT ASSIGNED", ID:=1, Book:="Book1", Page:="Page1", User:="User1", urlPDF:="url1", Transcription:="Transcription1", Transcription_Text:=TranscriptionTextbox.Text, isMaster:=0, isLocked:=0, Timestamp:="2004-11-15 11:37:10", CreateDate:="2004-11-15 11:37:10")
                    ' Add the incremented page link back to the String
                    pgLink = "http://br.mdsa.net/BookReader/index.html?id=ce425&item=1&format=Bitonal#page/" & gbCurrentPage & "/mode/1up"
                    ' Declare Label 1
                    Label1.Text = "<object id=""BookViewer"" data=""" & pgLink & """ height=""500"" width=""1500""><embed src=""" & pgLink & """ width=""95%"" height=""100%""/>Error: Embedded data could not be displayed.</object>"
                    ' Redirect to the Global Current Page Number
                    Response.Redirect("~/WebForm1.aspx?page=" & gbCurrentPage)
                ElseIf nextPageSaveConfirmation = MsgBoxResult.No Then
                    ' Increment global current page number by one page
                    'gbCurrentPage += 1
                    ' Go to next page:
                    ' Add the incremented page link back to the String
                    pgLink = "http://br.mdsa.net/BookReader/index.html?id=ce425&item=1&format=Bitonal#page/" & gbCurrentPage & "/mode/1up"
                    ' Declare Label 1
                    Label1.Text = "<object id=""BookViewer"" data=""" & pgLink & """ height=""500"" width=""1500""><embed src=""" & pgLink & """ width=""95%"" height=""100%""/>Error: Embedded data could not be displayed.</object>"
                    ' Redirect to the Global Current Page Number
                    Response.Redirect("~/WebForm1.aspx?page=" & gbCurrentPage)
                End If
            End If
        End If
    End Sub
4

1 回答 1

1

正如错误消息所说,您不能从共享方法调用实例化 - 非共享成员。因此,例如考虑以下设置:

Public Class Testing

  Public Shared Function Test1() As String
      Dim value = Test2()
      return value
  End Function

  Public Function Test2() As String
      return "Some text..."
  End Function

End Class

这将不起作用,因为您试图从共享方法 Test1 中调用非共享方法 Test2。它不起作用的原因是因为方法 Test2 需要一个测试类的实例才能被调用 - 它只能通过该实例调用。要解决此问题,您有 2 个选项:

  1. 使方法 Test 2 也被共享。
  2. 按以下方式修复代码:

    Public Class Testing
    
      Public Shared Function Test1() As String
        Dim instance = New Testing()
        Dim value = instance.Test2()
        return value
      End Function
    
      Public Function Test2() As String
        return "Some text..."
      End Function
    
    End Class
    
于 2013-08-02T14:42:01.777 回答