2

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.

2
  • Why are you using Response.Write? Never seen someone do it like that. Create a control instance and add it to the page. Oct 28, 2012 at 6:43
  • I am using Response.Write because I am creating site, in this site data received from Database and it's show on Site with button with every rows which retrieve from Database Table, If you have any other Idea for this then tell me. Oct 28, 2012 at 16:08

2 Answers 2

1

I found my answer,answer is below

Default.aspx.cs Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class dynamicimage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string str = @"<asp:ImageButton ID=""dynoimage"" ImageUrl=""~/images/about01.jpg"" runat=""server"" oncommand=""clickme"" commandname=""btn"" />";
        Control c = ParseControl(str);
        form1.Controls.Add(c);
        ((ImageButton)Page.FindControl("dynoimage")).Command += new CommandEventHandler(clickme);
    }

    protected void clickme(object sender,CommandEventArgs e)
    {
        Response.Write("Image clicked");
        Label1.Text = "Image clicked";
    }
}

and here is the Default.aspx Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicimage.aspx.cs" Inherits="dynamicimage" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <p>
        <asp:Label ID="Label1" runat="server" Text="before click"></asp:Label>
    </p>
    </form>
</body>
</html>
0

You have got the approach wrong, your first Response.Write("...") not the Response.Write("

Read this aritcle to use controls in ASP.NET dynamically.

http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx

4
  • Mr. Guru Kara, I think you not read full question, I want to create dynamic LinkButton for image without ID so I am using this way and it's working but event not work, without click event it's working properly,It's working for all tag with this method. Give me any way so event can work by this way. Oct 28, 2012 at 15:43
  • Events wont work unless you re-add the control in post backs to the control hierarchy in Pre_init event. Read this article fully then go for implementation cause dynamic controls in ASP.NET is not as easy as it seems, LINK==weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-2800_Part-1_2900.aspx
    – Guru Kara
    Oct 29, 2012 at 2:34
  • Mr. Guru Kara, you link is not working, when I open it, it's show me page not found. Oct 29, 2012 at 6:44
  • Just google "truly understanding dynamic controls asp.net" you should get a asp.net webblog called infinite loop
    – Guru Kara
    Oct 29, 2012 at 9:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.