C# 从 div onclick 调用 ASP.net 函数/方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17875489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call ASP.net function / method from div onclick
提问by Louis van Tonder
I have a basic url redirect on a DIV's onclick.
我在 DIV 的 onclick 上有一个基本的 url 重定向。
<div onclick="location.href = 'page.aspx';">
I have a need to do some work server side, rather than just a redirect, when this Div is clicked.
单击此 Div 时,我需要在服务器端做一些工作,而不仅仅是重定向。
How do I call one of my code behind functions from the DIV onclick? The DIV is the parent control inside a usercontrol. I suppose I can "fix" it by using a literal control and writing a query string into the onclick, but I would use that as a last resort as I don't want to use a query string.
如何从 DIV onclick 调用我的函数背后的代码之一?DIV 是用户控件内的父控件。我想我可以通过使用文字控件并将查询字符串写入 onclick 来“修复”它,但我将使用它作为最后的手段,因为我不想使用查询字符串。
Are there any other ways of catching a DIV click in code behind that I may have missed?
是否还有其他方法可以在我可能错过的代码中捕获 DIV 单击?
采纳答案by Abhishek Jain
You can have a button whose display can be 'none'. Handle click of Div on client side and then from there fire the Click of that button and handle the thinks Server Side on the Click Event of the button.
您可以拥有一个显示可以为“无”的按钮。在客户端处理 Div 的单击,然后从那里触发该按钮的单击,并在按钮的单击事件上处理服务器端。
<asp:Button runat="server" id="btnHidden" style="display:none" onclick="btnHidden_OnClick" />
<div onclick="javascript:DivClicked(); return true;"></div>
<script>
function DivClicked()
{
var btnHidden = $('#<%= btnHidden.ClientID %>');
if(btnHidden != null)
{
btnHidden.click();
}
}
</script>