Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call ASP.net function / method from div onclick

I have a basic url redirect on a DIV's onclick.

<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.

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.

Are there any other ways of catching a DIV click in code behind that I may have missed?

like image 768
Louis van Tonder Avatar asked Jul 26 '13 07:07

Louis van Tonder


1 Answers

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.

<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>
like image 55
Abhishek Jain Avatar answered Oct 21 '22 18:10

Abhishek Jain