简体   繁体   English

客户端和服务器端事件不同时触发

[英]Client side and server side events not firing simultaneously

I have a asp.net button control. 我有一个asp.net按钮控件。 My requirement is such that upon clicking the button, it should be disabled and fire a server side event simultaneously. 我的要求是,在单击按钮时,应该禁用它并同时触发服务器端事件。 I am disabling the button by 我正在禁用按钮

onclientclick = "this.disabled = true;"

and calling the server side method by 并通过调用服务器端方法

onclick = "someserversidemethod"

But the thing is that button is getting disabled and not firing the server side event. 但问题是按钮被禁用而没有触发服务器端事件。 I want both client side and server side events to fire simultaneously. 我希望同时触发客户端和服务器端事件。 Please help. 请帮忙。

Keep what you have and also set the property UseSubmitBehavior="false" . 保留你拥有的东西,并设置属性UseSubmitBehavior="false" This will force the button to use the asp.net postback mechanism, rather than the inbuilt browser mechanism. 这将强制按钮使用asp.net回发机制,而不是内置的浏览器机制。

See here for more details 有关详细信息,请参见此处

EDIT 编辑

Browsers as standard will always submit a form when you click on a <input type="submit"> control (which is what is rendered to the HTML when you use a <asp:Button> ). 当您单击<input type="submit">控件(使用<asp:Button>时呈现给HTML的内容)时,标准浏览器将始终提交表单。 ASP.NET uses this feature to initiate the postback to the server. ASP.NET使用此功能启动回发到服务器。

The problem you have is that if the button is disabled (like you are doing in your javascript) then the browser will not submit the form (ie the postback will not happen). 你遇到的问题是,如果按钮被禁用(就像你在javascript中那样),那么浏览器将不会提交表单(即不会发生回发)。

By using the attribute UseSubmitBehavior="false" you are telling ASP.NET to ignore the fact that browsers can do the submit themselves, and instead the button should use the JavaScript postback methods provided by ASP.NET (the same methods that are used on AutoPostback="true" for things like <asp:Checkbox> etc). 通过使用属性UseSubmitBehavior="false"您告诉ASP.NET忽略浏览器可以自己提交的事实,而按钮应该使用ASP.NET提供的JavaScript回发方法(使用的方法相同)对于<asp:Checkbox>等内容, AutoPostback="true"

The javascript postback ignores the fact that you have just set the control as disabled,meaning the control will be disabled on screen but the postback will still happen. javascript postback忽略了你刚刚将控件设置为禁用的事实,这意味着控件将在屏幕上禁用,但回发仍将发生。

I hope that makes sense 我希望这是有道理的

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

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