PlaceHolder ImageButton not working?

Hello all.  The attached code renders my image button but the Click event wont fire off and the button is not posting back at all.  Any idea?
private void Page_Load(object sender, System.EventArgs e)
		{
	ImageButton btnUD = new ImageButton();
 
			PlaceHolder plc1 = new PlaceHolder();
 
			btnUD.Click += new ImageClickEventHandler(btnUD_Click);
			btnUD.ImageUrl = "../images/buttons/button_update.jpg";
 
			StringBuilder sbAddBookItems = new StringBuilder("");
			Literal lit1 = new Literal();
			Literal lit2 = new Literal();
			sbAddBookItems.Append("<div style='text-align:right;'>");
			lit1.Text = sbAddBookItems.ToString();
			plc1.Controls.Add(btnUD);
			this.Controls.Add(lit1);
			this.Controls.Add(plc1);
			sbAddBookItems = new StringBuilder();
			sbAddBookItems.Append("</div>");
			lit1 = new Literal();
			lit1.Text = sbAddBookItems.ToString();
			this.Controls.Add(lit1);
}
 
protected void btnUD_Click(object sender, ImageClickEventArgs e)
		{
string test = "test this";
}

Open in new window

sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the_bachelorCommented:
I can't mess with no C#
but the the  VB.NET code below works me. Maybe you can convert it easily to C#

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim plc1 As PlaceHolder = New PlaceHolder
        Dim imgButton As ImageButton = New ImageButton
        imgButton.ImageUrl = "~/fantasy.gif"
        AddHandler imgButton.Click, AddressOf imgButton_Clicked
        Me.form1.Controls.Add(imgButton)
    End Sub
 
    Protected Sub imgButton_Clicked(ByVal sender As Object, ByVal arg As System.Web.UI.ImageClickEventArgs)
        Response.Write("Boo!")
    End Sub

Open in new window

0
the_bachelorCommented:
i went back looking at your code a bit and I changed it as shown in the snippet, it works for me
    private void Page_Load(object sender, System.EventArgs e)
    {
        ImageButton btnUD = new ImageButton();
 
        PlaceHolder plc1 = new PlaceHolder();
 
        btnUD.Click += new ImageClickEventHandler(btnUD_Click);
        btnUD.ImageUrl = "../images/buttons/button_update.jpg";
 
        StringBuilder sbAddBookItems = new StringBuilder("");
        Literal lit1 = new Literal();
        Literal lit2 = new Literal();
        sbAddBookItems.Append("<div style='text-align:right;'>");
        lit1.Text = sbAddBookItems.ToString();
        plc1.Controls.Add(btnUD);
        this.form1.Controls.Add(lit1);
        this.form1.Controls.Add(plc1);
        sbAddBookItems = new StringBuilder();
        sbAddBookItems.Append("</div>");
        lit1 = new Literal();
        lit1.Text = sbAddBookItems.ToString();
        this.Controls.Add(lit1);
    }
 
    protected void btnUD_Click(object sender, ImageClickEventArgs e)
    {
        string test = "test this";
        this.lbl.Text = test; 
       
    }
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sbornstein2Author Commented:
its not working with the code above, however I dont have a form I am just using this.controls.   Should I place a form tag in the ASCX?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

sbornstein2Author Commented:
ok it works alone actually.  Its something in this code I have where I render a bunch of dynamic controls.  I call this function first in my page load and then what you have above.
private void RenderAddBookItems()
		{
			try
			{
				//Get Add Book Items based on Package
				int intPackageID = 1;
				DataSet dsAddBookItems = ServicesStoreDriver.GetPackageResultSet(intPackageID);
 
				if (dsAddBookItems.Tables[0].Rows.Count > 0)
				{
					//Setup main stringbuilder to create table of book items
					StringBuilder sbAddBookItems = new StringBuilder("");
					sbAddBookItems.Append("<table cellSpacing='1' cellPadding='0' width='550px' border='0'>");
					string currentDisplayID = "";
					string prevDisplayID = "";
					string displayID = "";
					string displayText = "";
					string ctrlType = "";
					int count = 1;
 
					//Setup first question then loop
					sbAddBookItems.Append("<tr>");
					if (dsAddBookItems.Tables[0].Rows[0]["IconName"] != null)
					{	
						string strImagePath = "images/icons/" + dsAddBookItems.Tables[0].Rows[0]["IconName"].ToString();
						sbAddBookItems.Append("<td valign='top' align='center' width='50px'><img src='" + strImagePath + "' /></td>");
					}
					else
					{
						sbAddBookItems.Append("<td></td>");
					}
				
					if (dsAddBookItems.Tables[0].Rows[0]["PackageItemDisplayId"] != null)
					{
						currentDisplayID = dsAddBookItems.Tables[0].Rows[0]["PackageItemDisplayId"].ToString();
						displayID = dsAddBookItems.Tables[0].Rows[0]["PackageItemDisplayId"].ToString();
						displayText = "<strong>" + dsAddBookItems.Tables[0].Rows[0]["DisplayText"].ToString() + "</strong>";
					}
 
					sbAddBookItems.Append("<td valign='top' width='400px'>" + displayText + "</td>");
					sbAddBookItems.Append("<td width='50px'></td>");
					sbAddBookItems.Append("<td width='50px'></td>");
					sbAddBookItems.Append("</tr>");
	
					RadioButton rbl = new RadioButton();
					CheckBox cbl = new CheckBox();
 
					Literal lit = new Literal();
					bool perwordflag = false;
					foreach (DataRow row in dsAddBookItems.Tables[0].Rows)
					{
						prevDisplayID = displayID;
						displayID = row["PackageItemDisplayId"].ToString();
 
						if (currentDisplayID == displayID)
						{
							sbAddBookItems.Append("<tr>");
							sbAddBookItems.Append("<td width='50px'></td>");
							if(row["PackageItemPriceDesc"] != null)
							{
								if(row["PackageItemPriceDesc"].ToString() == "per word")
								{
									sbAddBookItems.Append("<td valign='top' width='400px'>" + row["PackageItemDesc"].ToString() + " - per word @ words</td>");
								}
								else
								{
									sbAddBookItems.Append("<td valign='top' width='400px'>" + row["PackageItemDesc"].ToString() + "</td>");
								}
							}
							else
							{
								sbAddBookItems.Append("<td valign='top' width='400px'>" + row["PackageItemDesc"].ToString() + "</td>");
							}
							string strPrice = row["NetPrice"].ToString();
							double d = double.Parse(strPrice);
							if(row["PackageItemPriceDesc"] != null)
							{
								if(row["PackageItemPriceDesc"].ToString() == "per word")
								{
									string formattedPrice = d.ToString("#.000");
									sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'>$" + formattedPrice + "</td>");
								}
								else
								{
									string formattedPrice = d.ToString("#.00");
									sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'>$" + formattedPrice + "</td>");
								}
							}
							else
							{
								string formattedPrice = d.ToString("#.00");
								sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'>$" + formattedPrice + "</td>");
							}
						}
						else
						{
							if(perwordflag == true)
							{
								sbAddBookItems.Append("<tr>");
								sbAddBookItems.Append("<td></td>");
								sbAddBookItems.Append("<td style='text-align:right;'>Words: </td>");
								sbAddBookItems.Append("<td style='text-align:left;' colspan='2'>");
								TextBox tbx = new TextBox();
								tbx.Width = Unit.Pixel(30);
								tbx.ID = "tbx" + count + "_" + prevDisplayID;  //i.e. ID = rbl1_23
								myControls.Add(tbx.ID);
								lit = new Literal();
								lit.Text = sbAddBookItems.ToString();
								this.Form1.Controls.Add(lit);
								this.Form1.Controls.Add(tbx);
								sbAddBookItems = new StringBuilder();
								sbAddBookItems.Append("</td>");
								sbAddBookItems.Append("</tr>");
								perwordflag = false;
							}
 
							
							if(row["PackageItemPriceDesc"] != null)
							{
								if(row["PackageItemPriceDesc"].ToString() == "per word")
								{
									perwordflag = true;
								}
							}
 
							sbAddBookItems.Append("<tr>");
							if (row["IconName"] != null)
							{	
								string strImagePath = "images/icons/" + row["IconName"].ToString();
								sbAddBookItems.Append("<td valign='top' align='center' width='50px'><img src='" + strImagePath + "' /></td>");
							}
							else
							{
								sbAddBookItems.Append("<td width='50px'></td>");
							}
 
							if (row["DisplayText"] != null)
							{
								displayText = "<strong>" +  row["DisplayText"].ToString() + "</strong>";
							}
 
							sbAddBookItems.Append("<td valign='top' width='400px'>" + displayText + "</td>");
							sbAddBookItems.Append("<td width='50px'></td>");
							sbAddBookItems.Append("<td width='50px'></td>");
							sbAddBookItems.Append("</tr>");
 
							sbAddBookItems.Append("<tr>");
							sbAddBookItems.Append("<td width='50px'></td>");
							if(row["PackageItemPriceDesc"] != null)
							{
								if(row["PackageItemPriceDesc"].ToString() == "per word")
								{
									sbAddBookItems.Append("<td valign='top' width='400px'>" + row["PackageItemDesc"].ToString() + " - per word @ words</td>");
								}
								else
								{
									sbAddBookItems.Append("<td valign='top' width='400px'>" + row["PackageItemDesc"].ToString() + "</td>");
								}
							}
							else
							{
								sbAddBookItems.Append("<td valign='top' width='400px'>" + row["PackageItemDesc"].ToString() + "</td>");
							}
							string strPrice = row["NetPrice"].ToString();
							double d = double.Parse(strPrice);
							if(row["PackageItemPriceDesc"] != null)
							{
								if(row["PackageItemPriceDesc"].ToString() == "per word")
								{
									string formattedPrice = d.ToString("#.000");
									sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'>$" + formattedPrice + "</td>");
								}
								else
								{
									string formattedPrice = d.ToString("#.00");
									sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'>$" + formattedPrice + "</td>");
								}
							}
							else
							{
								string formattedPrice = d.ToString("#.00");
								sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'>$" + formattedPrice + "</td>");
							}
 
							currentDisplayID = row["PackageItemDisplayId"].ToString();
							count++;
						}
						
						ListItem li = new ListItem();
 
						if (row["ControlType"] != null)
						{
							//Need to check if this is a checkbox or radio button question
							if (row["ControlType"].ToString() == "single")
							{
								ctrlType = "radio";
							}
							else
							{
								ctrlType = "check";
							}
						}
 
						sbAddBookItems.Append("<td valign='top' width='50px'>");
 
						string strPriceControl = row["NetPrice"].ToString();
						double dctrl = double.Parse(strPriceControl);
						string formattedPriceControl = "0.00";
 
						if(row["PackageItemPriceDesc"] != null)
						{
							if(row["PackageItemPriceDesc"].ToString() == "per word")
							{
								formattedPriceControl = dctrl.ToString("#.000");
							}
							else
							{
								formattedPriceControl = dctrl.ToString("#.00");
							}
						}
						else
						{
							formattedPriceControl = dctrl.ToString("#.00");
						}
 
						if (ctrlType == "radio")
						{
							rbl.ID = "rbl" + count + "_" + prevDisplayID;  //i.e. ID = rbl1_23
							rbl.GroupName = "DisplayGroup" + displayID;
							rbl.Attributes.Add("Price", formattedPriceControl);
							rbl.Attributes.Add("ItemID", row["ItemID"].ToString());
 
							if(row["PackageItemPriceDesc"].ToString() == "per word")
							{
								rbl.Attributes.Add("PriceType", "perword");
								rbl.Attributes.Add("BoxName", "tbx" + count + "_" + displayID);
							}
							else
							{
								rbl.Attributes.Add("PriceType", "normal");
							}
							myControls.Add(rbl.ID);
							myControls.Add(rbl.Attributes);
							lit.Text = sbAddBookItems.ToString();
							this.Form1.Controls.Add(lit);
							this.Form1.Controls.Add(rbl);
							rbl = new RadioButton(); //reset the RadioButton to a new one
							rbl.CssClass = "ListIndent";
							lit = new Literal();
						}
						else
						{
							cbl.ID = "cbl" + count + "_" + prevDisplayID;  //i.e. ID = rbl1_23
							cbl.Attributes.Add("Price", formattedPriceControl);
							cbl.Attributes.Add("ItemID", row["ItemID"].ToString());
							myControls.Add(cbl.ID);
							lit.Text = sbAddBookItems.ToString();
							this.Form1.Controls.Add(lit);
							this.Form1.Controls.Add(cbl);
							cbl = new CheckBox(); //reset the CheckBoxList to a new one
							cbl.CssClass = "ListIndent";
							lit = new Literal();
						}
					
						sbAddBookItems = new StringBuilder();
 
						sbAddBookItems.Append("</td>");
						sbAddBookItems.Append("</tr>");
 
					}
		
					if(perwordflag == true)
					{
						sbAddBookItems.Append("<tr>");
						sbAddBookItems.Append("<td></td>");
						sbAddBookItems.Append("<td style='text-align:right;'>Words: </td>");
						sbAddBookItems.Append("<td style='text-align:left;' colspan='2'>");
						TextBox tbx = new TextBox();
						tbx.Width = Unit.Pixel(30);
						tbx.ID = "tbx" + count + "_" + prevDisplayID;  //i.e. ID = rbl1_23
						myControls.Add(tbx.ID);
						lit = new Literal();
						lit.Text = sbAddBookItems.ToString();
						this.Form1.Controls.Add(lit);
						this.Form1.Controls.Add(tbx);
						sbAddBookItems = new StringBuilder();
						sbAddBookItems.Append("</td>");
						sbAddBookItems.Append("</tr>");
						perwordflag = false;
					}
 
					Label lblTotal = new Label();
					lblTotal.Text = "$0.00";
					lblTotal.ID = "TotalLabel";
					
					sbAddBookItems = new StringBuilder();	
					sbAddBookItems.Append("<tr>");
					sbAddBookItems.Append("<td width='50px'></td>");
					sbAddBookItems.Append("<td valign='top' width='400px' style='text-align:right;'><strong>Total: </strong></td>");
					sbAddBookItems.Append("<td valign='top' width='50px' style='text-align:right;'><strong>"); 
					lit = new Literal();
					lit.Text = sbAddBookItems.ToString();
					this.Form1.Controls.Add(lit);
					this.Form1.Controls.Add(lblTotal);
					sbAddBookItems = new StringBuilder();
					sbAddBookItems.Append("</strong></td>");
					sbAddBookItems.Append("<td></td>");
					sbAddBookItems.Append("</tr>");
					sbAddBookItems.Append("</table>");
					sbAddBookItems.Append("</form>");
					lit = new Literal();
					lit.Text = sbAddBookItems.ToString();
					this.Form1.Controls.Add(lit);	
 
				}
				dsAddBookItems.Dispose();
			}
			catch (Exception ex)
			{
				RecordError(ex, "RenderAddBookItems");
			}
 
		}

Open in new window

0
sbornstein2Author Commented:
I found it.  I had a close form tag in that code causing the problem.  Its all set thanks alot for your help.
0
sbornstein2Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.