Click here to Skip to main content
14,920,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the problem is that I have created a click event of link button category_Click and inside this click event I have created multiple dynamic controls and I created a click event of an image button Image_Click and now the issue is that the Category_Click event is firing but the Image_Click event is not firing. please help me.

aspx page code:-

ASP.NET
 <%@ 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>
<asp:Content ID="Content2" ContentPlaceHolderID="categories" runat="server">
    <asp:Panel ID="Panel2" runat="server"></asp:Panel>   
</asp:Content>
<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>
    <asp:Panel ID="Panel3" runat="server">
        <div class="data">          
                    <div class="image">
                        <asp:Image ID="Image1" runat="server" Cssclass="Img" />
                    </div>
                    <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>                          
                            <div class="shipping">
                             <p>Available</p>
                            <p>Ships within 4-6 Business Days</p>
                            <p>Rs.39 shipping in India per item and low cost Worldwide.</p>
                            </div>                            
                        </div>                        
                        <asp:Button ID="Button1" runat="server" Text="Buy Now" class="atc"/>
                     </div>                   
     </div>    
     <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>  
      </div>
    </asp:Panel>
</asp:Content>


aspx.cs page code:-

C#
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;
                con2.Open();
                SQL2 = "Select distinct Book_category from Book_List";
                da2 = new SqlDataAdapter(SQL2, con2);
                ds2 = new DataSet();
                DataTable dt2 = new DataTable();
                da2.Fill(ds2);
                num2 = ds2.Tables[0].Rows.Count;
                HtmlGenericControl header = new HtmlGenericControl("div");
                header.Attributes.Add("class", "header");
                Panel2.Controls.Add(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);
                    header.Controls.Add(category);
                }
        }
        protected void Category_Click(object sender, EventArgs e)
        {
            label = ((LinkButton)sender).Text;
            Label1.Text = label;
            con.Open();
            SQL = "Select * from Book_List where Book_category='" + label + "'";
            da = new SqlDataAdapter(SQL, con);
            ds = new DataSet();
            da.Fill(ds);
            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");
            Panel1.Controls.Add(Book);
            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";
                Book.Controls.Add(myDiv);
                myDiv.Controls.Add(image);
                myDiv.Controls.Add(content);
                content.Controls.Add(id);
                content.Controls.Add(bookname);
                content.Controls.Add(money);
                content.Controls.Add(wishlist);
            }
        }
        protected void Image_Click(object sender, ImageClickEventArgs e)
        {
            Panel3.Visible = true;
            Panel2.Visible = false;
            //ImageButton image = sender as ImageButton;
            //Response.Redirect("genre_content.aspx");           
            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();
        }
    }
}


What I have tried:

I have tried using submit behaviour and causes validation but it didn't work I also tried creating all the dynamic elements in a function and then calling it but it gives a null error.
Posted
Updated 6-Apr-21 4:41am

1 solution

Page_Load is too late to create dynamic controls. You will overwrite the controls on every postback, so their events will never fire. Create the controls in Page_Init instead.

Quote:
C#
SQL = "Select * from Book_List where Book_category='" + label + "'";
Using string concatenation to build a SQL query can and will lead to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900