Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating image buttons dynamically on button click. I have handled its click event and done some coding in it. But the problem is that on clicking on image button the click event method of image button is not calling. So please tell me how to handle it.

Thank u.

=========
From JSOP: Don't use all-caps in your messages.

code is here:

        con.Open();
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        PlaceHolder1.Controls.Clear();
        Table tbl = new Table();
        PlaceHolder1.Controls.Add(tbl);

        for ( i = 0; i < 5; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < 2; j++)
            {
                TableCell tc = new TableCell();
                HyperLink h1 = new HyperLink();
                Image img = new Image();
                ImageButton imgbt = new ImageButton();
                if (j == 0)
                {
                    imgbt.ImageUrl = ds.Tables[0].Rows[i]["picture"].ToString();
                    imgbt.Height = 50;
                    imgbt.Width = 50;
                      imgbt.Click += new ImageClickEventHandler(imgbt_Click);

                   ds.Tables[0].Rows[i]["picture"].ToString();

                   tc.Controls.Add(imgbt);
                }
                else
                {
                    h1.Text = ds.Tables[0].Rows[i]["Name"].ToString();
                    // Add the control to the TableCell
                    tc.Controls.Add(h1);
                }

                h1.ForeColor = System.Drawing.Color.Black;
                h1.NavigateUrl = ds.Tables[0].Rows[i]["PageUrl"].ToString();

                // Add the TableCell to the TableRow
                tr.Cells.Add(tc);
            }

            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
        }
    }
    catch { }
}

void imgbt_Click(object sender, ImageClickEventArgs e)
{
   //throw new NotImplementedException();
    Response.Write("welcome");
    string str1 = "";
    string str2 = "'" + ds.Tables[0].Rows[i]["Name"] + "'";
    string str3 = "'" + ds.Tables[0].Rows[i]["Adress"] + "'";

    str1 = "'" + ds.Tables[0].Rows[i]["picture"] + "'";
    Page.ClientScript.RegisterStartupScript(this.GetType  (), "Test", "getpoint(" + ds.Tables[0].Rows[i]["Latitude"] + "," + ds.Tables[0].Rows[i]["Longitude"] + "," + str1 + "," + str2 + "," + str3 + ");", true);
}
Posted
Updated 20-Apr-10 8:01am
v4

You have to create your dynamic imagebutton on every postback.then only your event will fire
 
Share this answer
 
Before doing what JSOP has said about posting code, just verify that you are creating the image button at runtime even on postback and assigning the handler too!

Looks like, the event handler is not attached again once postback happened.
 
Share this answer
 
If you don't show us your code, we can't help you.
 
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