This is a migrated thread and some comments may be shown as answers.

Client-sice template items

4 Answers 93 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Roman Krsko
Top achievements
Rank 1
Roman Krsko asked on 10 Nov 2010, 12:08 PM
Hi,

I have empty combobox with template created on server side. 
I need add item with javascript on client-side, but I need use template items (checkbox)
If I add new item, I have only clasic combobox item.

class CheckBoxTemplate : ITemplate
{
    string ColName = "cboCol1";
    const string head = "<div onclick=\"StopPropagation(event)\">";
    const string tail = "</div>";
 
    public void InstantiateIn(Control container)
    {
        CheckBox chkControl = new CheckBox();
        chkControl.ID = "chkControl";
        chkControl.CssClass = "";
        chkControl.Attributes.Add("onclick", "onCheckBoxClick('" + _controlClientId + "')");
        chkControl.DataBinding += new EventHandler(this.chkControl_DataBind);
        container.Controls.Add(new LiteralControl(head));
        container.Controls.Add(chkControl);
        container.Controls.Add(new LiteralControl(tail));
    }
 
    public void chkControl_DataBind(Object sender, EventArgs e)
    {
        CheckBox chkControl = (CheckBox)sender;
        RadComboBoxItem radComboItem = (RadComboBoxItem)chkControl.NamingContainer;
        chkControl.Text = " " + DataBinder.Eval(radComboItem.DataItem, ColName).ToString();
    }
}

Javascript add items
var xmlDoc = xmlDoc.firstChild;
if (xmlDoc.childNodes.length > 0) {
    var combo = $find(comboid);
    combo.trackChanges();

    for (x = 0; x < xmlDoc.childNodes.length; x++) {
        try {
            sCboID = xmlDoc.childNodes[x].childNodes[0].childNodes[0].nodeValue;
            sCboText = xmlDoc.childNodes[x].childNodes[1].childNodes[0].nodeValue;               
            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text(sCboText);
            comboItem.set_value(sCboID);
            combo.get_items().add(comboItem);
        }
        catch (err) { }
    }
    combo.commitChanges();
}

Thanks for help.
Best regards
Roman

4 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 10 Nov 2010, 06:12 PM
Hi Roman Krsko,

Templates are rendered on the server. Since RadComboBox does not know what is actually being rendered inside its Items it cannot do so on the client by itself, so you need to manually supply the Template content on the client when you add Items.

So I suggest you add the exact same HTML you render on the server in the InstantiateIn method of the Template on the client inside the item element (item.get_element()). You can use jQuery ($telerik.$ for the jQuery version we ship) to add the HTML.

I hope this helps.

Greetings,
Simon
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Roman Krsko
Top achievements
Rank 1
answered on 30 Nov 2010, 09:42 PM
Hi Simon,

can you send me sample (source code sample) with item element (item.get_element()) and jQuery using, because I don't understand, what you mind.
Thanks very much.

Best regards
Roman
0
Simon
Telerik team
answered on 07 Dec 2010, 07:16 PM
Hello Roman Krsko,

Yes, below is a sample code snippet showing a function accepting an Item object and puts some HTML inside its element:
function addTemplateToItem(item) {
    // Use jQuery that we ship with RadControls for ASP.NET AJAX.
    var itemElement = $telerik.$(item.get_element());
    itemElement.empty(); // Remove the existing HTML in the Item.
    itemElement.append("<div>some content</div>"); // Add the HTML of the Template inside the Item's element.
}

Please note that the append function also accepts an element, so you can directly pass it some already created element.

You can achieve the same without using jQuery, e.g.
function addTemplateToItem(item) {
    item.get_element().innerHTML = "<div>some content</div>";
}

You can read more about jQuery and the empty and append functions here:
I hope this helps.

Greetings,
Simon
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Roman Krsko
Top achievements
Rank 1
answered on 10 Dec 2010, 01:13 PM
Thanks Simon, I will trying it.
Roman
Tags
ComboBox
Asked by
Roman Krsko
Top achievements
Rank 1
Answers by
Simon
Telerik team
Roman Krsko
Top achievements
Rank 1
Share this question
or