[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

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

0
sbornstein2
Asked:
sbornstein2
  • 4
  • 2
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now