简体   繁体   English

从Javascript调用VB.NET子例程?

[英]Call VB.NET subroutine from Javascript?

I'm able to call javascript from VB all day long. 我可以整天从VB调用javascript。 There's a ton of examples out there. 那里有很多例子。

Problem is, I need to call a VB.NET function from javascript, no two ways around it. 问题是,我需要从javascript调用VB.NET函数,没有两种解决方法。 This is a bit harder to find. 这很难找到。

Anyone know of any resources? 有人知道任何资源吗?

Thanks, 谢谢,

Jason 杰森

BTW -- Web/Page methods won't work. 顺便说一句-Web / Page方法将不起作用。 I have to be able to reference a listbox and it's selected items when the VB code-behind fires. VB代码隐藏触发时,我必须能够引用一个列表框及其选定的项目。 Thanks! 谢谢!

这是一篇有趣的文章,可能会帮助您如何强制执行回发并执行一些代码: http : //weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx

Can you explain some more ? 你能解释更多吗?

If the VB code behind fires, then surely a post back has occured ? 如果后面的VB代码被触发,那么肯定发生了回发吗? And the function has fired, so why would you want to call the function again ? 并且该函数已触发,那么为什么还要再次调用该函数? Maybe you need to rethink how you are getting the data from the post back, back to the page ? 也许您需要重新考虑如何从帖子中获取数据,回到页面上?

Remember that you're still dealing in web requests/responses. 请记住,您仍在处理Web请求/响应。 A browser sends a request to your web server, the web server builds a response and sends it back to the browser. 浏览器将请求发送到您的Web服务器,该Web服务器生成响应并将其发送回浏览器。 Each request/response pair are independent of the others: once the response is sent to the browser, the web server disposes of all server resources used to fill the request. 每个请求/响应对都彼此独立:将响应发送到浏览器后,Web服务器将处理所有用于填充请求的服务器资源。 Network traffic is relatively slow, so by the time any javascript code runs, your server-side page object is already long gone. 网络流量相对较慢,因此在运行任何 JavaScript代码时,您的服务器端页面对象已经很久了。 It just doesn't exist any more. 它只是不存在了。

ASP.Net webforms goes to a lot of effort via viewstate to hide all this from you, but that's still the way it is. ASP.Net Web窗体通过viewstate进行了大量工作以隐藏所有这些内容,但这仍然是这样。 If you want to call to server-side VB.Net code and you don't want to use a normal postback, you can do that via ajax (webmethod). 如果您想调用服务器端VB.Net代码,并且不想使用普通的回发,则可以通过ajax(webmethod)来实现。 The trick is that you have to write the code to send and response to any information you require, because you've thrown all the helps built in by asp.net out the window. 诀窍是您必须编写代码以发送和响应所需的任何信息,因为您已经把asp.net内置的所有帮助抛到了窗外。

The other option here is to use javascript to trigger a postback. 这里的另一个选项是使用javascript触发回发。 That is relatively easy: you can do it by just calling the __doPostBack() function. 这是相对容易的:您可以通过调用__doPostBack()函数来实现。 The hard part is that you're not actually connected to a server event, so you'll need code in your page load to detect your specific request and react appropriately. 困难的部分是您实际上没有连接到服务器事件,因此您将需要页面加载中的代码来检测您的特定请求并做出适当的反应。

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

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