[英]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:
这是真正发生的情况:
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.