如何使用Public Shared Sub调用Visual Basic中受保护的子goToRequestedPage()中找到的功能

时间:2013-08-02 14:11:08

标签: vb.net

这是我的问题:

我在点击'功能上使用了Javascript #Next'来调用Public Shared Sub NextPage()方法,并在“If-else”语句中呈现我的bookviewer。

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

AutoSaveCheckBoxLabel1Response都声明了以下错误:“无法在没有显式的情况下从共享方法或共享成员初始化程序中引用类的实例成员班级的实例。“

正如您所看到的,我希望弹出一个消息框,要求用户保存,以允许用户转到下一页。用户可以转到下一页,但是如何实现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()):< / p>

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

1 个答案:

答案 0 :(得分:1)

正如错误消息所示,您无法从共享方法中调用instanced - not shared成员。例如,考虑一下这个设置:

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需要一个Testing类的实例才能被调用 - 它只能通过该实例调用。要解决此问题,您有两个选择:

  1. 也可以共享方法测试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