admin管理员组文章数量:1277377
For a requirement, I need to fire both client side and server side click events of a button in ASP.NET. Is that possible? I have tried like the below. But it does not fire client side click event.
[ASPX]
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="return click();" />
[Script]
function click()
{
alert("hi")
}
For a requirement, I need to fire both client side and server side click events of a button in ASP.NET. Is that possible? I have tried like the below. But it does not fire client side click event.
[ASPX]
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="return click();" />
[Script]
function click()
{
alert("hi")
}
Share
Improve this question
asked Jan 22, 2013 at 11:27
RGRRGR
1,5712 gold badges23 silver badges37 bronze badges
5
- your code seems ok it should work. Can you tell the behavior you are getting after button clicked. – Gaurav Rajput Commented Jan 22, 2013 at 11:39
- It fires Server side click event. But not client side event. – RGR Commented Jan 22, 2013 at 11:40
- All events are client side. There's no such thing as a server-side event. ASP's syntax just makes it appear that way. One of the many reasons, I avoid ASP. – Beetroot-Beetroot Commented Jan 22, 2013 at 11:46
- Yes. The error is in my code!. I specified client-side event as 'click();' . That's the mistake i had done. Thanks for your much help anyways :). I feel like silly now!! – RGR Commented Jan 22, 2013 at 12:02
- I have renamed the client-side click event as 'disable()' and it works now. Thanks all for your help. – RGR Commented Jan 22, 2013 at 12:08
3 Answers
Reset to default 9Yes, it's possible. If the client side Javascript returns true
then the server-side method will get called.
For example:
function click()
{
return confirm("Do you want to continue?");
}
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="click();" />
function click()
{
alert("hi");
this.click();
}
The client side function must be called in your code. Only thing is depending on what it returns the server side function will get called:
function Click() {
alert('Hi');
if (somecondition) {
return true;
}
return false;
}
Also check your server side event handler if it is named correct as you mention in your code "disable". I think this isnt proper name.
Also check if you really need CausesValidation attribute? as this would have been for your validater which might no longer needed as client side function is manually called.
本文标签: javascriptHow to fire both client side and server side click events of buttonStack Overflow
版权声明:本文标题:javascript - How to fire both client side and server side click events of button? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741201963a2357390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论