简体   繁体   English

VB.NET调用Javascript函数。 如何在VB.NET完成之前完成javascript函数?

[英]VB.NET calls Javascript function. How do I get javascript function to complete before VB.NET completes?

In a web application I am using a button_Click method in VB.Net to occur when a button is clicked. 在Web应用程序中,我使用VB.Net中的button_Click方法来在单击按钮时发生。

I have the following line at the top of my VB.NET method: 我的VB.NET方法顶部有以下几行:

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myFunction", "myFunction();", True)

I want that script to finish before my VB.NET script carries on. 我希望该脚本在我的VB.NET脚本进行之前完成。 Basically, I have validation in my javascript that I want to complete before the VB.NET takes the "validated" data and inserts it into a database. 基本上,在VB.NET接收“已验证”数据并将其插入数据库之前,我要在我的JavaScript中完成验证。

This betrays a misunderstanding of how web forms work. 这背离了对Web表单如何工作的误解。 Here's what really happens: 这是真正发生的情况:

  1. A user requests your page from their browser for the first time 用户首次从浏览器中请求您的页面
  2. Your web server runs the ASP.Net page life cycle in order to send an html response 您的Web服务器运行ASP.Net页面生命周期,以便发送html响应
  3. The web server destroys the page class instance it used to complete the request. Web服务器销毁用于完成请求的页面类实例
  4. The response from the server arrives and is rendered by the user's browser. 来自服务器的响应到达并由用户的浏览器呈现。
  5. The user clicks your button, resulting in a new http request. 用户单击您的按钮,从而产生一个新的http请求。
  6. The browser destroys the existing html DOM . 浏览器破坏了现有的html DOM
  7. The request arrives at the web server, which then runs the full ASP.Net life cycle again, including the Page_Load method . 请求到达Web服务器,然后Web服务器再次运行完整的ASP.Net生命周期,包括Page_Load方法
  8. This time the data included with the request indicates to ASP.Net that it should also run your button's Click code. 这次,请求中包含的数据向ASP.Net指示它也应该运行按钮的Click代码。
  9. The button registers the javascript to run when the page loads in the browser. 当页面加载到浏览器中时,该按钮注册要运行的javascript。
  10. The page lifecycle completes, and ASP.Net sends it's HTML response back to the browser. 页面生命周期完成,ASP.Net将其HTML响应发送回浏览器。
  11. ASP.Net destroys the page class instance again. ASP.Net再次销毁页面类实例。
  12. The response arrives at the browser, which renders it from scratch by creating a whole new html DOM . 响应到达浏览器,通过创建一个全新的html DOM从头开始呈现它。
  13. The page's javascript load event fires, and some javascript included with ASP.Net pages kicks off the javascript startup script registered by the button. 该页面的javascript加载事件将触发,并且ASP.Net页面随附的一些javascript会启动该按钮注册的javascript启动脚本。

I need to point out some things about this process, namely that order between steps 3 and 4, steps 6 and 7, and steps 11 and 12 are accurate. 我需要指出有关此过程的一些信息,即步骤3和4,步骤6和7之间以及步骤11和12之间的顺序是正确的。 When there is working page visible in the browser, the server has already moved on and destroyed anything used to create that page (except Session variables). 当浏览器中有可见的工作页面时,服务器已经继续移动并销毁了用于创建该页面的所有内容(Session变量除外)。 While VB.Net code is running, the browser doesn't even have a page to show yet. 在运行VB.Net代码时,浏览器甚至没有要显示的页面。

What you should learn from this is that at time the javascript runs, not only has the VB.Net method already finished , but the entire page class was already destroyed. 您应该从中学到的是,在运行javascript时,不仅VB.Net方法已经完成 ,而且整个页面类都已被破坏。 There's an idea of continuity here for both the browser's web page and the VB.Net Page class instance that just doesn't happen. 对于浏览器的网页和VB.Net Page类实例,这里都存在连续性的想法,但是这种情况不会发生。 It's just nice that all this happens in a way that is mainly transparent to the user. 很好的是,所有这些操作对用户来说都是透明的。

Fortunately, there are some things you can do to avoid this full process. 幸运的是,您可以采取一些措施来避免整个过程。 You might look into using an UpdatePanel for part of your page, changing the button to trigger a WebMethod, or translating more of the VB.Net code into javascript in the first place. 您可能会考虑将UpdatePanel用于页面的一部分,更改按钮以触发WebMethod,或者首先将更多的VB.Net代码转换为javascript。 However, all of these will likely require significant re-thinking of how your page is going to work. 但是,所有这些可能都需要重新考虑页面的工作方式。 In this case, you might find and a Validation control best fits your needs. 在这种情况下,您可能会发现并且验证控件最适合您的需求。

This is assuming that myFunction is a javascript function existing on your client side. 假设myFunction是客户端上存在的javascript函数。 It will call myFunction on the client side. 它将在客户端调用myFunction

<asp:Button ID="btntest" runat="server" Text="Add Record"/>
<asp:CustomValidator ID="myCustomValidator" runat="server" ControlToValidate="someControl" ErrorMessage="Validation Error" ClientValidationFunction="myFunction"></asp:CustomValidator>

This is assuming that you javascript is doing some validation as well. 这是假设您的javascript也正在做一些验证。 It would look something like this. 看起来像这样。 If args.IsValid = false , then the validator won't allow a postback and the vb.net code won't execute. 如果args.IsValid = false ,则验证器将不允许回发,并且vb.net代码将不会执行。 This is the point of a validator. 这就是验证者的观点。

function myFunction(sender, args) {
    var someControl = document.getElementById(sender.controltovalidate).control;

    //Let's assume someControl is a textbox and we don't want it bigger than 10
    if (someControl.value > 10) {
        args.IsValid = false;
    } else {
        args.IsValid = true;
    }
}

Hopefully, this gets you going. 希望这可以帮助您前进。 Let me know if something isn't working right and you need more help. 让我知道是否有问题,您需要更多帮助。

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

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