Skip to main content Skip to footer

Adding Template Controls to C1Toolbar

Working with C1ToolBar in design mode, dragging and dropping controls onto a Page's design surface and adding template items using smart tag, it's easy to forget that it's just a convenience. Anything you do graphically in Visual Studio always generates text in some file somewhere. When you add a template control to any toolbar item, some code is generated that instantiates the control and adds it to the page. You can add templates to C1ToolBar items at runtime, using the ItemTemplate property. This property is of type ITemplate, so you must assign an object that implements that interface as a value. The C1ToolBar items should be dynamically added so that templates can be defined at run time.

To create a dynamic template column

1. Create a template class that implements the ITemplate interface of the System.Web.UI namespace. For general information on creating dynamic templates, see Creating Web Server Control Templates Dynamically (http://msdn.microsoft.com/en-us/library/y0h809ak%28v=vs.71%29.aspx). 2. Optionally, pass into the class's constructor a value that the class can use to determine what type of template to create (ItemTemplate, EditItemTemplate, and so on). 3. In the class, implement the InstantiateIn method (the only member of the ITemplate interface). This method provides a way to insert an instance of text and controls into the specified container. The following example shows a template class for a dynamic template control. The constructor accepts one parameter which specifies the ID property of the template control. Because this is a template for a C1ToolBar control, the class includes code to create an EditTemplate containing a C1ComboBox control.

private class EditTemplate : ITemplate  

{  

string ID;  

public EditTemplate(string id)  

{  

this.ID = id;  

}  

public void InstantiateIn(Control container)  

{  

C1ComboBox combobox = new C1ComboBox();  

combobox.ID = ID;  

combobox.EmptyText = "Select";  

combobox.DropDownPositioningMode = PositioningMode.Center;  

combobox.Enabled = true;  

combobox.AutoPostBack = true;  

combobox.TemplateItemHighlighted = true;  

combobox.WindowCollisionDetection = true;  

C1ComboBoxItem item; //= new C1ComboBoxItem();  

for (int i = 1; i < 100; i++)  

{  

item = new C1ComboBoxItem();  

item.Text = i.ToString();  

item.Value = i.ToString();  

combobox.Items.Add(item);  

}  

combobox.SelectedIndexChanged += new C1ComboBoxEventHandler(combobox_SelectedIndexChanged);  

container.Controls.Add(combobox);  

}  

void combobox_SelectedIndexChanged(object sender, C1ComboBoxEventArgs args)  

{  

}  

}

After you have created the class for a dynamic template column, you can use it to assign template control to the C1Toolbar items at run time.

To use dynamic template control

1. Create an instance of the EditTemplate class. 2. Create an instance of your dynamic template, passing it an item type value if appropriate. 3. Assign the instance to one of the template properties of the ItemTemplate object. The following example shows how to add dynamic template column to add C1ComboBox to the C1ToolBarDropDownList item of C1ToolBar control. In this example, the templates are instantiated during the page load.

protected void Page_Load(object sender, EventArgs e)  

{  

C1ToolBar toolbar = new C1ToolBar();  

C1ToolBarButton btn = new C1ToolBarButton();  

btn.Text = "ToolbarButton";  

btn.ID = "button1";  

toolbar.Items.Add(btn);  

C1ToolBarDropDownList ddl = new C1ToolBarDropDownList();  

ddl.ItemTemplate = new EditTemplate("C1ComboBox1");  

toolbar.Items.Add(ddl);  

this.PlaceHolder1.Controls.Add(toolbar);  

}

Download Sample

MESCIUS inc.

comments powered by Disqus