简体   繁体   English

如何从JavaScript代码向VB.NET方法发送参数?

[英]How can I send parameters to VB.NET method from JavaScript code?

I have vb.net method and i call it from JS code i can't send parameter to this method and here is sample: JS Code : 我有vb.net方法,我从JS代码中调用它,我无法向该方法发送参数,这是示例:JS代码:

function OnSave() 
    {
        var sign = document.FORM1.SigPlus1.Signature();
        <%Save(sign) %>
    }

VB method : VB方法:

Public Sub Save(ByVal obj As Object)
    Dim obj1 As New PFSIGNATURELib.SigniShellSignature
    obj1.SignatureBytes = obj
    obj1.SaveBitmapToFile(CurDir() & "\sign1.bmp", 200, 200)
    signImg.Src = CurDir() & "\sign1.bmp"

End Sub

Well actually you can do it in an "easy" way: 好吧,实际上您可以通过“简单”的方式来做到这一点:

Add a hidden LinkButton to you page: 将一个隐藏的LinkBut​​ton添加到您的页面:

<asp:LinkButton runat="server" id="MyPostBackHelper" style="display: none;" />

then in your javascript: 然后在您的JavaScript中:

function OnSave() 
{
    var sign = document.FORM1.SigPlus1.Signature();
    __doPostBack(<%= MyPostBackHelper.UniqueID %>, sign);
}

and then in the codebehind: 然后在后面的代码中:

Public Sub MyPostBackHelper_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyPostBackHelper.Click
  Dim obj1 As New PFSIGNATURELib.SigniShellSignature
  obj1.SignatureBytes = Request.Form("__EVENTARGUMENT")
  obj1.SaveBitmapToFile(CurDir() & "\sign1.bmp", 200, 200)
  signImg.Src = CurDir() & "\sign1.bmp"
End Sub

There is no easy way to do it. 没有简单的方法可以做到这一点。

Your best bet is to create a web service call, and have Javascript call the web service using ASP.NET Ajax. 最好的选择是创建一个Web服务调用,并使用ASP.NET Ajax让Javascript调用该Web服务。

Here is a way to do that. 是一种方法。

Is it possible to do something like 是否可以做类似的事情

<%Save(%>sign<%) %> ? <%Save(%>sign<%) %>吗?

Could you not use the ajax libraries built into .net 3.5 and mark your method as a web Method 您不能使用.net 3.5中内置的Ajax库并将您的方法标记为Web方法吗?

       <WebMethod()> _
    Public Shared Function SaveParamsData
End sub

Then use javascript to call that method. 然后使用javascript调用该方法。

PageMethods.SaveParamsData(ReportItemPramName, MyMethod_Result);

hmm, could you not cause the javascript to post the data back to the page. 嗯,您能不能使javascript将数据发布回页面。 then check to see if there is postdata on page load, if there is, then save it 然后检查是否存在页面加载后数据,如果存在,则保存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM