Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi..i want to call code behind function on html button click event in datalist view..
please help me.....
Posted

Try some thing like this

Code behind
C#
public void logout()
{
}

client page
<script type="text/javascript">
    window.onunload = unloadEvents;
    function unloadEvents()
    {
      <% logout(); %>
    }
</script>
 
Share this answer
 
v2
Comments
Tejas Vaishnav 10-Apr-12 0:29am    
That thing not work on button click event. it will call code behind function when page get render complete or page load.
for calling server side function from html button you need to call like this.

you need to make that function static and mark that with webmethod attribute.

then write javascript function on your design side that will fire a ajax request to call that function from your code behind as you call any webservice function from your side using jquery/javascript ajax request.

Your javascript function look like this.
JavaScript
<script type="text/javascript">
function DeleteData() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/DeleteData",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert("Your function called sucessfully");
            //if your Codebehind method return something then that data or result will be accessed using .d attribute of msg
            //like this...

            //alert("Your method return data is ..." + msg.d);
        },
        error: function (msg) {
            alert("There's some error in calling your function.");
        }
    });
    return false;
}
</script>


and your code behind function like this...

C#
[WebMethod]
public static void DeleteData()
{
       //your code
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900