Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I need to create Image Button dynamically, and have to generate click event.. But while clicking on image button, the click event is not generating and hiding the Image button.. What is the problem in my code..

This is my code..

 ImageButton imgBtn; 

protected void Button1_Click(object sender, EventArgs e)
        {
            DBConnections.GetCon  obj= new DBConnections.GetCon();

            MySqlConnection con = new MySqlConnection(obj.getConStr());
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM <tabel>",con);
            MySqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                imgBtn = new ImageButton();
                //Image imgBtn = new Image();
                
                
                imgBtn.ID = dr.GetInt32(1).ToString();
                if (dr.GetInt32(2) == 0)
                {
                    imgBtn.ImageUrl = "~/Users/SeatImg/1.jpg";
                }
                else
                {
                    imgBtn.ImageUrl = "~/Users/SeatImg/3.jpg";
                }



                imgBtn.Click += new ImageClickEventHandler(img_Click);
                this.Panel2.Controls.Add(imgBtn);
            }
            con.Close();

        }

        protected void img_Click(object sender, ImageClickEventArgs e)
        {
            imgBtn.PostBackUrl = "www.google.com";
        }
Posted

1 solution

Hi,
The reason is due to the ASP.NET page is Stateless.

Check this link this has solution for your problem.
Dynamically Created Controls in ASP.NET[^]
 
Share this answer
 

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



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