【问题标题】:How to call function in .vb file via aspx page?如何通过aspx页面调用.vb文件中的函数?
【发布时间】:2019-02-05 10:53:43
【问题描述】:

我已经创建了 webform aspx 文件,并且我有一个生成 pdf 然后上传到我的表单的按钮。当我单击提交按钮时,需要在 vb 文件中调用一个函数,例如 DocuSign 的 CreateEnvelpoeAPI。无论如何,如何通过aspx页面调用函数?

//form.aspx.vb
Protected Sub btSubmit_Click(sender As Object, e As EventArgs) Handles btSubmit.Click
    Response.Redirect("CreateEnvelopeAPI.vb") // I expecte to call function n in here.
End Sub

// CreateEnvelopeAPI.vb
Public Class CreateEnvelopeAPI

Public Function Init() As String
    Dim username As String = ""
    Dim password As String = ""
    Dim integratorKey As String = ""

    ' initialize client for desired environment (for production change to www)
    Dim apiClient As New ApiClient("https://demo.docusign.net/restapi")
    Configuration.[Default].ApiClient = apiClient

    ' configure 'X-DocuSign-Authentication' header
    Dim authHeader As String = (Convert.ToString((Convert.ToString((Convert.ToString("{""Username"":""") & username) + """, ""Password"":""") & password) + """, ""IntegratorKey"":""") & integratorKey) + """}"
    Configuration.[Default].AddDefaultHeader("X-DocuSign-Authentication", authHeader)

    ' we will retrieve this from the login API call
    Dim accountId As String = Nothing

    '''//////////////////////////////////////////////////////////////
    ' STEP 1: LOGIN API        
    '''//////////////////////////////////////////////////////////////

    ' login call is available in the authentication api 
    Dim authApi As New AuthenticationApi()
    Dim loginInfo As LoginInformation = authApi.Login()

    ' parse the first account ID that is returned (user might belong to multiple accounts)
    accountId = loginInfo.LoginAccounts(0).AccountId

    ' Update ApiClient with the new base url from login call
    apiClient = New ApiClient(loginInfo.LoginAccounts(0).BaseUrl)

    Return accountId
End Function
Public Sub CreateEnvelopeAPI()
    Dim accountId As String = Init()

    Dim fileBytes As Byte() = File.ReadAllBytes("Invoice.pdf")

    Dim envDef = New EnvelopeDefinition()
    envDef.EmailSubject = "[DocuSign C# SDK] - Custom Fields"
    envDef.Status = "sent"


    envDef.CustomFields = New CustomFields()

    Dim textCustomField = New TextCustomField()
    textCustomField.Name = "TransactionId"
    textCustomField.Value = "KTI456"

    Dim textCustomFields = New List(Of TextCustomField)()
    textCustomFields.Add(textCustomField)
    envDef.CustomFields.TextCustomFields = textCustomFields

    ' Add a recipient to sign the documeent
    Dim signer As New Signer()
    signer.Email = ""
    signer.Name = ""
    signer.RecipientId = "1"

    signer.Tabs = New Tabs()
    signer.Tabs.SignHereTabs = New List(Of SignHere)()

    Dim signHereTab = New SignHere()
    signHereTab.DocumentId = "1"
    signHereTab.PageNumber = "1"
    signHereTab.XPosition = "100"
    signHereTab.YPosition = "100"
    signer.Tabs.SignHereTabs.Add(signHereTab)

    envDef.Recipients = New Recipients()
    envDef.Recipients.Signers = New List(Of Signer)()
    envDef.Recipients.Signers.Add(signer)

    ' Add a document to the envelope
    Dim doc As New Document()
    doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes)
    doc.Name = "Invoice.pdf"
    doc.DocumentId = "1"

    envDef.Documents = New List(Of Document)()
    envDef.Documents.Add(doc)


    Dim envelopesApi As New EnvelopesApi()
    Dim envelopeSummary As EnvelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef)
    Console.WriteLine(envelopeSummary)
End Sub

End Class

【问题讨论】:

    标签: asp.net vb.net webforms docusignapi


    【解决方案1】:

    调用函数就像调用 VB 中的任何其他函数一样。它位于 ASP.NET 页面的上下文中的事实对此没有任何影响。

    Protected Sub btSubmit_Click(sender As Object, e As EventArgs) Handles btSubmit.Click
        Dim api = new CreateEnvelopeAPI() 'instantiate the class
        api.CreateEnvelopeAPI() 'call a function
    End Sub
    

    (注意。这是假设 CreateEnvelopeAPI.vb 文件是您项目的一部分,并且该类位于同一命名空间中。如果不是这种情况,那么您只需要包含该文件和/或添加 Imports 语句以在您的 aspx 代码中包含命名空间。)

    附: Response.Redirect 用于将浏览器发送到另一个页面(即另一个 HTTP URL),而不是在代码中调用函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      相关资源
      最近更新 更多