简体   繁体   中英

linkbutton onclick event not firing in asp net

I know this is a repeated question however, it seems a different situation. I have a link button on my aspx page whose event protected void LinkButton1_Click(object sender, EventArgs e) is not firing

 <form>
   <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" causesvalidation="false"><a href="Product.aspx?id=<%#Eval("ItemCode") %>">LinkButton</a></asp:LinkButton>
 </form>

Here <asp:LinkButton has a green underline stating Element 'asp:LinkButton' is missing required attribute 'runat' When I put runat="Server" it gives me error that:

Control 'Repeater1_LinkButton1_0' of type 'LinkButton' must be placed inside a form tag with runat=server.

And when I put <Form runat="Server"> it gives me

A page can have only one server-side Form tag.

I am not sure if that is creating an issue. Please help

Answering your question from comments.

<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="yourCommand" CommandName="AddToCart" CommandArgument='<%#Eval("itemID")%>'>Click here</asp:LinkButton>

And in your code-behind:

protected void yourCommand(object sender, CommandEventArgs e)
{
    int myID = int.Parse(e.CommandArgument.ToString());
}

May i know Why you are putting a reference tag (href)inside the link button tag. I think this is not right, try this,

 <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" runat="server" causesvalidation="false"></asp:LinkButton>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM