Check Checkbox in radcombobox itemtemplate

2 posts, 0 answers
  1. adam
    adam avatar
    15 posts
    Member since:
    Sep 2010

    Posted 03 Sep 2010 Link to this post

    I used some code found on this forum to implement checkboxes in a combobox itemtemplate. everything works fine apart from i want the saved items checkbox to be checked when the dropdown is viewed.
    any ideas?

    here is the front end

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="TopFiveArticlesSelector.ascx.cs" Inherits="CMSFormControls_TopFiveArticlesSelector" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
     <script type="text/javascript">
                var hiddenfld = ("<%= this.Hidden.ClientID %>");
                var cancelDropDownClosing = false;
                 
                function StopPropagation(e)
                {
                    //cancel bubbling
                    e.cancelBubble = true;
                    if (e.stopPropagation)
                    {
                        e.stopPropagation();
                    }
                }
     
                function onDropDownClosing()
                {
                    cancelDropDownClosing = false;
                }
     
                function onCheckBoxClick(chk)
                {
                    var combo = $find("<%= RadComboBox2.ClientID %>");
                    //holds the text of all checked items
                    var text = "";
                    //holds the values of all checked items
                    var values = "";
                    //get the collection of all items
                    var items = combo.get_items();
                    //enumerate all items
                    for (var i = 0; i < items.get_count(); i++)
                    {
                        var item = items.getItem(i);
                        //get the checkbox element of the current item
                        var chk1 = $get(combo.get_id() + "_i" + i + "_chk1");
                        if (chk1.checked)
                        {
                            text += item.get_text() + ",";
                            values += item.get_value() + ",";
                            //alert(item.get_value());
                        }
                    }
                    //remove the last comma from the string
                    text = removeLastComma(text);
                    values = removeLastComma(values);
                     
                 
                    if (text.length > 0)
                    {
                        //set the text of the combobox                   
                        combo.set_text(text);
                        document.getElementById(hiddenfld).value = values;                  
                       
                    }
                    else
                    {
                        //all checkboxes are unchecked
                        //so reset the controls
                        combo.set_text("");              
                    }
                }
     
                //this method removes the ending comma from a string
                function removeLastComma(str)
                {
                    return str.replace(/,$/,"");
                }
     
    </script>
    <asp:HiddenField ID="Hidden" runat="server"/>
     <telerik:RadComboBox ID="RadComboBox2" runat="server" Width="400" AllowCustomText="true" DataValueField="DocumentID" DataTextField="Title" EmptyMessage="Select upto 5 articles" OnClientDropDownClosed="onDropDownClosing" >
        <ItemTemplate>
            <div onclick="StopPropagation(event)" class="combo-item-template">
                <asp:CheckBox runat="server" ID="chk1" Text='<%#Eval("Title")%>' onclick="onCheckBoxClick(this)" />           
            </div>
        </ItemTemplate>
    </telerik:RadComboBox>

    here is the backend

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;
    using CMS.SiteProvider;
    using CMS.CMSHelper;
    using CMS.GlobalHelper;
    using Telerik.Web.UI;
     
    public partial class CMSFormControls_TopFiveArticlesSelector : CMS.FormControls.FormEngineUserControl
    {
        private DataTable dt = null;
     
        protected void Page_Load(object sender, EventArgs e)
        {
            ArticleItems();
        }
     
        /// <summary>
        /// Gets or sets field value
        /// </summary>
        public override Object Value
        {
            get
            {
                return Hidden.Value.ToString();           
            }
            set
            {
                // Ensure drop down list options
                ArticleItems();
     
                Hidden.Value = System.Convert.ToString(value);
     
                string[] RelatedID = Hidden.Value.Split(new char[] { ',' });
                           
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int e = 0; e < RelatedID.Length; e++)
                    {
                        if (dt.Rows[i]["DocumentId"].ToString() == RelatedID[e].ToString())
                        {
                            RadComboBox2.Text += dt.Rows[i]["Title"].ToString() + ",";
                            CheckBox checkbox = (CheckBox)RadComboBox2.Items[i].FindControl("chk1");
                            checkbox.Checked = true;
                        }
                    }
                }
     
            }
        }
     
        /// <summary>
        /// Returns true if some color is selected. If no, it returns false and displays an error message.
        /// </summary>
        public override bool IsValid()
        {
            if (RadComboBox2.Text != "")
            {
                return true;
            }
            else
            {
                // Set form control validation error message
                this.ValidationError = "Please Select Tags For This Article";
                return false;
            }
        }
     
        protected void ArticleItems()
        {
            DataSet ds = TreeHelper.SelectNodes("/%", false, "CriticalCare.Conclusion;CriticalCare.Literature;CriticalCare.Theory", "", " ", -1, false);
            dt = new DataTable();
     
            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    dt.Merge(ds.Tables[i]);
                }          
                RadComboBox2.DataSource = dt;
                RadComboBox2.DataBind();
     
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CheckBox checkbox = (CheckBox)RadComboBox2.Items[i].FindControl("chk1");
                    checkbox.ToolTip = dt.Rows[i]["DocumentID"].ToString();
                }
            }     
        }
    }
  2. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 04 Sep 2010 Link to this post

    Hi adam,

    Your code looks fine. Can you please verify whether the code checking the CheckBoxes actually executes, i.e.

    if (dt.Rows[i]["DocumentId"].ToString() == RelatedID[e].ToString())

    is true?

    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
Back to Top