Asp.net服务器控制服务器端/客户端的相同事件处理顺序

时间:2021-08-22 16:10:11

I have an asp.net server control (with the asp: in its definition). The button has been set to do post back.

我有一个asp.net服务器控件(在其定义中使用asp:)。该按钮已设置为回发。

On the server side, I have the on click event handler e.g btnSave_click()

在服务器端,我有点击事件处理程序,例如btnSave_click()

On the client side, I have a javascript function to be invoked on the click event e.g btnSave.Attributes.Add("onclick","javascript: return CheckIsDirty();")

在客户端,我有一个javascript函数可以在click事件上调用,例如btnSave.Attributes.Add(“onclick”,“javascript:return CheckIsDirty();”)

Am not sure which order these two will be executed. Because I want first on the client side to warn of any data entry fields that are not yet filled-out before actually saving any data.

我不确定这两个订单的执行顺序。因为我想首先在客户端警告在实际保存任何数据之前尚未填写的任何数据输入字段。

Any help?

4 个解决方案

#1


3  

First client side, second server-side.

第一个客户端,第二个服务器端。

So you can use it.

所以你可以使用它。

I also use it in some cases, like:

我也在某些情况下使用它,例如:

close.Attributes["OnClick"] = "return confirm('Are you sure?')";

In this case if the user presses 'No' then the server-side event handler does not even play a role.

在这种情况下,如果用户按下“否​​”,则服务器端事件处理程序甚至不起作用。

#2


2  

The trick here is to set this global variable "Page_IsValid" false if your test fails and this will stop the post back.

这里的诀窍是如果您的测试失败,则将此全局变量“Page_IsValid”设置为false,这将停止回发。

Read this page http://msdn.microsoft.com/en-us/library/aa479045.aspx which explains both server side and client Validation. There is sone good code example you can use.

阅读此页面http://msdn.microsoft.com/en-us/library/aa479045.aspx,它解释了服务器端和客户端验证。你可以使用一些好的代码示例。

#3


1  

The way you are setting your onClick JavaScript event will actually prevent it from posting back as you are overwritten the ASP.NET event handler. The correct way to accomplish the validation you are intending is to:

设置onClick JavaScript事件的方式实际上会阻止它在覆盖ASP.NET事件处理程序时回发。完成您想要的验证的正确方法是:

btnSave.Attributes.Add("onclick", "CheckIsDirty();" + GetPostBackEventReference(btnSave).ToString());

Notice that you append the result of GetPostBackEventReference, so that in JavaScript you first call your CheckIsDirty() method and then call the ASP.NET postback method. Assuming your method returns true, then the button will post. If it returns false then it will not cause a post back.

请注意,您追加GetPostBackEventReference的结果,以便在JavaScript中首先调用CheckIsDirty()方法,然后调用ASP.NET回发方法。假设您的方法返回true,则该按钮将发布。如果它返回false,那么它不会导致回发。

Does that sound like what you are trying to accomplish?

这听起来像你想要完成的吗?

#4


0  

I think you need a much better understanding of what it means client side and what it means server side and how they all relate together. I've seen more and more developers make a mess of it.

我认为您需要更好地理解客户端的含义以及服务器端的含义以及它们如何相互关联。我看到越来越多的开发人员弄得一团糟。

Of course the client side will execute first in your case. Actually there's no way to execute it after the server code is executed (except if you do something manually). I'll try to give a brief explanation:

当然,客户端将在您的情况下首先执行。实际上,在执行服务器代码之后无法执行它(除非您手动执行某些操作)。我将尝试简要解释一下:

Whatever you have in your server, will generate some HTML on the client and the user is always interacting on the client. So you have a html button that the user is clicking. What the browser will do is execute the javascript associated with it or if no javascript is specified and the button is a submit button it will submit the form. if you check the generated html you will see that for the onclick event you will have the script you have added followed by some autogenerated script that actually will submit the form to the server. Your server side code will execute only if the page will be submitted.

无论您在服务器中拥有什么,都会在客户端上生成一些HTML,并且用户始终在客户端上进行交互。所以你有一个用户点击的html按钮。浏览器将执行的是执行与之关联的javascript,或者如果没有指定javascript,按钮是提交按钮,它将提交表单。如果你检查生成的html,你会看到onclick事件你会得到你添加的脚本,然后是一些自动生成的脚本,它实际上会将表单提交给服务器。只有在提交页面时才会执行服务器端代码。

#1


3  

First client side, second server-side.

第一个客户端,第二个服务器端。

So you can use it.

所以你可以使用它。

I also use it in some cases, like:

我也在某些情况下使用它,例如:

close.Attributes["OnClick"] = "return confirm('Are you sure?')";

In this case if the user presses 'No' then the server-side event handler does not even play a role.

在这种情况下,如果用户按下“否​​”,则服务器端事件处理程序甚至不起作用。

#2


2  

The trick here is to set this global variable "Page_IsValid" false if your test fails and this will stop the post back.

这里的诀窍是如果您的测试失败,则将此全局变量“Page_IsValid”设置为false,这将停止回发。

Read this page http://msdn.microsoft.com/en-us/library/aa479045.aspx which explains both server side and client Validation. There is sone good code example you can use.

阅读此页面http://msdn.microsoft.com/en-us/library/aa479045.aspx,它解释了服务器端和客户端验证。你可以使用一些好的代码示例。

#3


1  

The way you are setting your onClick JavaScript event will actually prevent it from posting back as you are overwritten the ASP.NET event handler. The correct way to accomplish the validation you are intending is to:

设置onClick JavaScript事件的方式实际上会阻止它在覆盖ASP.NET事件处理程序时回发。完成您想要的验证的正确方法是:

btnSave.Attributes.Add("onclick", "CheckIsDirty();" + GetPostBackEventReference(btnSave).ToString());

Notice that you append the result of GetPostBackEventReference, so that in JavaScript you first call your CheckIsDirty() method and then call the ASP.NET postback method. Assuming your method returns true, then the button will post. If it returns false then it will not cause a post back.

请注意,您追加GetPostBackEventReference的结果,以便在JavaScript中首先调用CheckIsDirty()方法,然后调用ASP.NET回发方法。假设您的方法返回true,则该按钮将发布。如果它返回false,那么它不会导致回发。

Does that sound like what you are trying to accomplish?

这听起来像你想要完成的吗?

#4


0  

I think you need a much better understanding of what it means client side and what it means server side and how they all relate together. I've seen more and more developers make a mess of it.

我认为您需要更好地理解客户端的含义以及服务器端的含义以及它们如何相互关联。我看到越来越多的开发人员弄得一团糟。

Of course the client side will execute first in your case. Actually there's no way to execute it after the server code is executed (except if you do something manually). I'll try to give a brief explanation:

当然,客户端将在您的情况下首先执行。实际上,在执行服务器代码之后无法执行它(除非您手动执行某些操作)。我将尝试简要解释一下:

Whatever you have in your server, will generate some HTML on the client and the user is always interacting on the client. So you have a html button that the user is clicking. What the browser will do is execute the javascript associated with it or if no javascript is specified and the button is a submit button it will submit the form. if you check the generated html you will see that for the onclick event you will have the script you have added followed by some autogenerated script that actually will submit the form to the server. Your server side code will execute only if the page will be submitted.

无论您在服务器中拥有什么,都会在客户端上生成一些HTML,并且用户始终在客户端上进行交互。所以你有一个用户点击的html按钮。浏览器将执行的是执行与之关联的javascript,或者如果没有指定javascript,按钮是提交按钮,它将提交表单。如果你检查生成的html,你会看到onclick事件你会得到你添加的脚本,然后是一些自动生成的脚本,它实际上会将表单提交给服务器。只有在提交页面时才会执行服务器端代码。