Click event inside a click event is not firing

Last Reply 2 days ago By dharmendr

Posted 5 days ago

I have created dynamic controls to display on my website according to the data in my database and now the issue is the click event created of the link button is firing but the click event I created of the image button is not firing.

 please help me guys I am stuck in this for a long time and I need to submit my project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace WebApplication1
    public partial class Categories : System.Web.UI.Page
        private string ide, SQL, SQL2, label;
        private int num, i, num2, j;
        private ImageButton image;
        private LinkButton bookname;
        private Label money;
        private Label id;
        private Button wishlist;
        private LinkButton category;
        private static DataSet ds, ds2;
        private static SqlDataAdapter da, da2;
        private static HtmlGenericControl Book;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
                Panel3.Visible = false;
                SQL2 = "Select distinct Book_category from Book_List";
                da2 = new SqlDataAdapter(SQL2, con2);
                ds2 = new DataSet();
                DataTable dt2 = new DataTable();
                num2 = ds2.Tables[0].Rows.Count;
                HtmlGenericControl header = new HtmlGenericControl("div");
                header.Attributes.Add("class", "header");
                for (j = 0; j < num2; j++)
                    category = new LinkButton();
                    category.Text = ds2.Tables[0].Rows[j]["Book_category"].ToString();
                    category.Attributes.Add("runat", "server");
                    category.Attributes.Add("CausesValidation", "false");
                    category.Click += new EventHandler(Category_Click);
        protected void Category_Click(object sender, EventArgs e)
            label = ((LinkButton)sender).Text;
            Label1.Text = label;
            SQL = "Select * from Book_List where Book_category='" + label + "'";
            da = new SqlDataAdapter(SQL, con);
            ds = new DataSet();
            num = ds.Tables[0].Rows.Count;
            //creating div element and putting all the elements ina  div called books
            Book = new HtmlGenericControl("div");
            Book.Attributes.Add("class", "books");
            for (i = 0; i < num; i++)
                //creating div element
                HtmlGenericControl myDiv = new HtmlGenericControl("div");
                myDiv.Attributes.Add("class", "myDiv");
                //creating image button
                image = new ImageButton();
                image.ImageUrl = ds.Tables[0].Rows[i]["Book_image"].ToString();
                image.CssClass = "Img";
                image.Attributes.Add("runat", "server");
                //image.UseSubmitBehaviour = false;
                image.Attributes.Add("CausesValidation", "false");
                //image.Attributes.Add("OnClick", "image_Click");
                //image.OnClientClick = Panel3;
               image.Click += new ImageClickEventHandler(Image_Click);
                //creating div inside myDiv
                HtmlGenericControl content = new HtmlGenericControl("div");
                content.Attributes.Add("class", "content");
                //creating a label to display id
                id = new Label();
                id.CssClass = "id";
                id.Text = ds.Tables[0].Rows[i]["Book_id"].ToString();
                id.Attributes.Add("runat", "server");
                //id.Click += new ImageClickEventHandler(id_Click);
                //creating a label for displaying name of the book
                bookname = new LinkButton();
                bookname.CssClass = "name";
                bookname.Text = ds.Tables[0].Rows[i]["Book_name"].ToString();
                bookname.Attributes.Add("runat", "server");
                //bookname.Click += new EventHandler(bookname_Click);
                //creating a label for displaying cost of the book
                money = new Label();
                money.CssClass = "cost";
                money.Text = "<br/> Rs " + ds.Tables[0].Rows[i]["Book_cost"].ToString();
                money.Attributes.Add("runat", "server");
                //creating a button to add the book to the wishlist
                wishlist = new Button();
                wishlist.Attributes.Add("runat", "server");
                wishlist.CssClass = "wishlist";
                wishlist.Text = "ADD TO WISHLIST";
        protected void Image_Click(object sender, ImageClickEventArgs e)
            Panel3.Visible = true;
            Panel2.Visible = false;
            //ImageButton image = sender as ImageButton;
            ide = ((ImageButton)sender).ImageUrl;
            for (i = 0; i < num; i++)
                if (ide == ds.Tables[0].Rows[i]["Book_image"].ToString())
                    Session["name"] = ds.Tables[0].Rows[i]["Book_name"].ToString();
                    Session["image"] = ds.Tables[0].Rows[i]["Book_image"].ToString();
                    Session["cost"] = ds.Tables[0].Rows[i]["Book_cost"].ToString();
                    Session["isbn"] = ds.Tables[0].Rows[i]["Book_isbn_no"].ToString();
                    Session["weight"] = ds.Tables[0].Rows[i]["Book_weight"].ToString();
                    Session["author"] = ds.Tables[0].Rows[i]["Book_author"].ToString();
                    Session["about"] = ds.Tables[0].Rows[i]["Book_about"].ToString();
                    Session["publisher"] = ds.Tables[0].Rows[i]["Book_publisher"].ToString();
                    Session["nop"] = ds.Tables[0].Rows[i]["No_of_pages"].ToString();
                    Session["language"] = ds.Tables[0].Rows[i]["Book_language"].ToString();
                    Session["available"] = ds.Tables[0].Rows[i]["Available_for"].ToString();
            Image1.ImageUrl = Session["image"].ToString();
            Name.Text = Session["name"].ToString();
            about.Text = Session["about"].ToString();
            cost.Text = Session["cost"].ToString();
            author.Text = Session["author"].ToString();
            isbn.Text = Session["isbn"].ToString();
            publisher.Text = Session["publisher"].ToString();
            nop.Text = Session["nop"].ToString();
            language.Text = Session["language"].ToString();
            weight.Text = Session["weight"].ToString();
            available.Text = Session["available"].ToString();


<%@ Page Title="" Language="C#" MasterPageFile="~/Homepage.Master" AutoEventWireup="true" CodeBehind="Categories.aspx.cs" Inherits="WebApplication1.Categories" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link rel="Stylesheet" href="Genre.css" />
     <link rel="Stylesheet" href="genre_content.css" />
<asp:Content ID="Content2" ContentPlaceHolderID="categories" runat="server">
    <asp:Panel ID="Panel2" runat="server"></asp:Panel>   
<asp:Content ID="Content3" ContentPlaceHolderID="main" runat="server">
    <asp:Panel ID="Panel1" runat="server">
        <h2><asp:Label ID="Label1" class="h2" runat ="server" ></asp:Label></h2><br/>        
    <asp:Panel ID="Panel3" runat="server">
        <div class="data">          
                    <div class="image">
                        <asp:Image ID="Image1" runat="server" Cssclass="Img" />
                    <div class="description"> 
                        <asp:Label ID="Name" runat="server" class="name"></asp:Label>  
                        <asp:Label ID="Label2" runat="server" Text="(Paperback)"></asp:Label>
                        <div class="cos-shipping">
                            <div class="cos">
                                 Rs.<asp:Label ID="cost" runat="server" CssClass="co" ></asp:Label>
                            <div class="shipping">
                            <p>Ships within <b>4-6 Business Days</b></p>
                            <p>Rs.39 shipping in India per item and low cost Worldwide.</p>
                        <asp:Button ID="Button1" runat="server" Text="Buy Now" class="atc"/>
     <div class="details">
         <h2>Book Details</h2>
                        <asp:Label ID="about" runat="server" CssClass="about" ></asp:Label> 
                        <p>Author: <asp:Label ID="author" runat="server" ></asp:Label>   </p>   
                        <p>ISBN: <asp:Label ID="isbn" runat="server" ></asp:Label>     </p>                      
                        <p>Pubisher: <asp:Label ID="publisher" runat="server" ></asp:Label>  </p>
                        <p>No of pages: <asp:Label ID="nop" runat="server" ></asp:Label>           </p>
                        <p>Language: <asp:Label ID="language" runat="server" ></asp:Label>  </p>     
                        <p>Weight: <asp:Label ID="weight" runat="server" ></asp:Label>      </p>
                        <p>Available For :<asp:Label ID="available" runat="server" ></asp:Label>   </p>  


You are viewing reply posted by: aparna 4 days ago.
Posted 4 days ago

But it is only showing how to create the events in the pageload but my issue is different I am generating dynamic controls click event in a dynamically created control click event that is not firing.