0

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

2
  • Probably you have one <form runat="server"> already defined in your aspx page or in master page. Commented Apr 26, 2017 at 11:32
  • I dont have a master page, and I looked for <form runat="server"> but its no where Commented Apr 26, 2017 at 11:33

2 Answers 2

2

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());
}
Sign up to request clarification or add additional context in comments.

3 Comments

If I use runat="Server" it gives same error Control 'Repeater1_LinkButton1_0' of type 'LinkButton' must be placed inside a form tag with runat=server.
Sorted the error but again its not firing LinkButton1_Command
I solved it by placng <form id="form1" runat="server"> just below the Body Tag, and used your method to get data all woking fine. Thanks
0

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>

2 Comments

I have this href for query string , so that i can save clicked item in cookies
how would I get the itemID ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.