Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to call a c# function in Html tag. I wrote the following code, but it throws error.
ASP.NET
 <script language="C#" runat="server">
        public void create_Session_For_ProductID(string id)
        {
            if (Session["Sess_PID"] != "")
            {
                Session["Sess_PID"] = Session["Sess_PID"] + id;
            }
            else
            {
                Session["Sess_PID"] = id;
            }

        }
    </script>

<%
        
         functions func=new functions();
         foreach (var item in func.GetProductList())
           { %>
          <div id="i<%:item.ProductID %>" class="item">
              <%--<img src="%3C%:%20Url.Content%28" />"/>--%>
              <img src="<%:item.ProductImage %>" width="140px" height="160px"/>
              <label class="title">IMG <%:item.ProductID%></label>
              <label class="price">Rs. <%:item.Price %></label>  
                         
               
              <a id="aref" href="MyCart.aspx"  runat="server"  önserverclick="create_Session_For_ProductID(<%:item.ProductID %>)"><span>Add To Cart</span></a>       
          </div>          
         <% } %>
Posted
v2
Comments
jim lahey 12-Jul-12 9:37am    
what kind of error?
rajesh@1989 12-Jul-12 9:42am    
Error 12 Invalid expression term '<'
Error 17 Invalid expression term ')'

1 solution

Without seeing any details of your error, I can only assume that it's the server side method signature that's the problem. Server side events have a different signature, for example:

C#
protected void Something_Click(object ssnder, EventArgs e){
    //your code here
}


And you'd call it like this:

ASP.NET
<a id="aref" href="MyCart.aspx" runat="server" onserverclick="Something_Click">Add To Cart</a> 


That still leaves you with the problem of passing the ProductId to the server side method. I wouldn't use a Html tag in that case. Use an ASP.net LinkButton and assign the ProductId to the CommandArgument property of the LinkButton.
 
Share this answer
 
Comments
rajesh@1989 12-Jul-12 10:00am    
thank you,

now i am using following method but e.commandArgument return <%item.ProductID%>,
<asp:LinkButton ID="LnBtn" runat="server"
CommandArgument='<% :item.ProductID %>' CommandName="click"
oncommand="LnBtn_Command">Add To Cart
jim lahey 12-Jul-12 10:15am    
Then you're assigning it wrong. I'd try and assign it from server side code.

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