Rank: Advanced Member Groups: Member
Joined: 12/19/2010 Posts: 47
|
Is there any sample code to do this? I create my controls dynamically and would like to optionally have the user add a tool tip. My users create their controls by dragging them onto a palette and then defining them. How can I dynamically create a tooltip for the dynamically created control. Any sample code would be appreciated.
In page_load, programatically: 1. Create control (simple textbox); 2. Add Tooltip to control.
Thanks in advance ...
|
Rank: Advanced Member Groups: Member
Joined: 12/19/2010 Posts: 47
|
This must be done with the EO.Web.ToolTip object using an ITemplate that instantiates in a label control. Assuming that TextBox control was created with the ID: tb_stname and a PlaceHolder exists on the page with the ID: ph_stname here is how the tooltip is created dynamically:
EO.Web.ToolTip tt = new EO.Web.ToolTip(); tt.ID = string.Format("tt_stname"); tt.For = "tb_stname"; tt.ContentTemplate = new ToolTipTemplate("Hello, here is the tooltip text"); ph_stname.Controls.Add(tt);
Here is the ToolTipTemplate class:
public class ToolTipTemplate : ITemplate { private string txt;
// *** Constructor used to pass string to label control public ToolTipTemplate(string txt) { this.txt = txt; }
// *** Instantiates ITemplate with Label public void InstantiateIn(System.Web.UI.Control container) { Label la = new Label(); la.ID = "Ffff"; la.Text = txt; container.Controls.Add(la); } }
|
Rank: Advanced Member Groups: Member
Joined: 12/19/2010 Posts: 47
|
There is a much easier way to do this without having to use an ITemplate. You can also use the BodyHtml and HeaderHtml attributes.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
Thanks for the updates and sharing. Dynamically creating a control is generic ASP.NET topic so there really isn't anything particular related to our controls. So you may want to search online on that topic and as you have noted, there are issues like ITemplate that may require additional code. In addition to that, you also must pay attention to the control's life cycle. There are two general rules:
1. The control must be created as early as possible. Some controls won't work well if it is added into the page too late --- an extremely example is if you create the control after the page has already been rendered, then obviously it has no effect; 2. If you need event from the control, you must create the control during the initial load (when Page.IsPostBack is false) and during the post back (when Page.IsPostBack is true) exactly the same way. In another word, you must re-create the control during post back. This is essential for server side event to fire properly;
Hope this helps. Please feel free to let us know if you still have any questions.
Thanks!
|