dynamic LinkButton OnClick event not working on ASP.Net

Posted by user1004472 on Stack Overflow See other posts from Stack Overflow or by user1004472
Published on 2012-10-28T05:53:27Z Indexed on 2012/10/30 5:02 UTC
Read the original article Hit count: 236

Filed under:
|

I want to create dynamic LinkButton for image, <img> tag is not working dynamically so I am using LinkButton with image.
I don't want to provide ID to LinkButton because I want to generate more LinkButton dynamically. I am using following code in Default.aspx

    <%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <script runat="server">   
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write(@"<asp:LinkButton runat=""server"" OnClick=""btn_click""><img src=""close-icon (1).png"" /></asp:LinkButton>");
            }

            public void btn_click(object sender, EventArgs e)
            {
                Response.Write("HELLO");
            }   
        </script>
    </head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

I also tried to write tag code in Default.aspx.cs file but not work.
It's showing me following error.

Error 1 'ASP.default_aspx' does not contain a definition for 'img_Click' and no extension method 'img_Click' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)

Please help me to solve this problem.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asplinkbutton