0

I have a div in asp.net webform:

<div  class="AddToCartHome" id='AddToCartHome_<%#Eval("id") %>' onClientClick="AddToCartHomeJS();" style="height:auto;width:auto;display:inline;">
    <img src="images/addtocart-orange-4_224x81.png" width="120px" height="30px" id="AddToCartButtonHome" onclick="AddToCartFromHome" />
</div>

It is part of a listview. I want to take the id of the list item from the id of the div.

I did:

$(".AddToCartHome").click(function AddToCartHomeJS() {
    var id = $(this).attr("id");
    document.getElementById('<%=hdnItemID.ClientID %>').value = id.split("_").pop();
    return true;
});

I use a hidden field for storing the item ID:

<asp:HiddenField ID="hdnItemID" runat="server" />

It is all working perfect. Now I want to execute a method in code behind for adding the item into session cart.

public void AddToCartFromHome()
{
       -----
       -----
       -----
 }

As you can see, I have written the onclick method in the div, onclick="AddToCartFromHome" but it is not being fired.

How can I do that?

user2538559
  • 59
  • 2
  • 9
  • [Here](http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/) is how to do it in C#, it should be easy enough to implement in VB. I'd suggest you set up an actual service, like Web API for instance, if possible but pagemethods as described at that link will work. – Crowcoder Mar 01 '15 at 11:53
  • How to call an ASP.NET c# method using javascript and the answer is: http://stackoverflow.com/a/7089956/586074 – Saykor Mar 01 '15 at 15:35

0 Answers0