简体   繁体   中英

Call VB.NET subroutine from Javascript?

I'm able to call javascript from VB all day long. 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. This is a bit harder to find.

Anyone know of any resources?

Thanks,

Jason

BTW -- Web/Page methods won't work. I have to be able to reference a listbox and it's selected items when the VB code-behind fires. 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 ? 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. A browser sends a request to your web server, the web server builds a response and sends it back to the browser. 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. Network traffic is relatively slow, so by the time any javascript code runs, your server-side page object is already long gone. 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. 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). 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.

The other option here is to use javascript to trigger a postback. That is relatively easy: you can do it by just calling the __doPostBack() function. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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