none
Event not working with ParseControl method RRS feed

  • Question

  • I want to add dynamic image button on site without ID of Image button so I am using ASP tag to generate dynamic controls with ParseControl method, It's working but event is not fired when click on Image Button

    Default.aspx.cs Code

    protected void Page_Load(object sender, EventArgs e)
            {
                string str = @"<asp:ImageButton runat=server ImageUrl=""~/close-icon (1).png"" OnClick=""click"" />";
                Control c = Page.ParseControl(str);
                form1.Controls.Add(c);
            }
    
    
            protected void click(object sender, ImageClickEventArgs e)
            {
                Response.Write("Image Clicl");
            }

    Default.aspx Code

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    
    <!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>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
        </div>
        </form>
    </body>
    </html>

    Please help me to solve my problem by giving code in solution.

    Monday, October 29, 2012 10:54 AM

Answers

  • I found my answer, answer is below

    Default.aspx.cs Code

    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 Code

    <%@ 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>





    Tuesday, October 30, 2012 4:54 AM