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

Using CheckBoxes for multi-item selection

4 Answers 152 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Minh
Top achievements
Rank 1
Minh asked on 11 Mar 2010, 11:28 AM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
    <script type="text/javascript">
    // NOTE - these two functions can be moved to the page
 function StopPropagation(e)
 {
        //cancel bubbling
     e.cancelBubble = true;
  if (e.stopPropagation)
        {
   e.stopPropagation();
  }
 }

 //this method removes the ending character from a string
 function removeLastDelimiter(str)
 {
     var len = str.length;
        if (len > 1)
         return str.substr(0, len - 1);   
     return str;
 }
 function testchange()
 {
     $find("<%= CB1.ClientID %>").set_text("MA5303");
 }
</script>
</telerik:RadScriptBlock>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
     <telerik:RadComboBox ID="CB1" runat="server" Skin="Office2007"
                    AllowCustomText="True" OnItemDataBound="CB1_ItemDataBound"
                    HighlightTemplatedItems="True">
                    <HeaderTemplate>
                        <asp:CheckBox runat="server" ID="SelectAll" />
                        <asp:Label runat="server" ID="hdrLabel" AssociatedControlID="SelectAll">Select All</asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <div onclick="StopPropagation(event)">
                            <asp:CheckBox runat="server" ID="chk1" />
                            <asp:Label runat="server" ID="label1" AssociatedControlID="chk1"><%# DataBinder.Eval(Container, "Text") %></asp:Label>
                        </div>
                    </ItemTemplate>
                    <CollapseAnimation Duration="200" Type="OutQuint" />
                </telerik:RadComboBox>
    </div>
    </form>
</body>
</html>


-----------------------------------------------------

private string delimiter = ",";

    public string Delimiter
    {
        get { return delimiter; }
        set { delimiter = value; }
    }
   
    public RadComboBox DDList
    {
        get { return this.CB1; }
    }

    protected override void OnInit(EventArgs e)
    {
        // Create the javascript functions with the client id appended to them.
        StringBuilder sb = new StringBuilder();
        sb.Append(@"<script type='text/javascript'>");
        sb.Append(@"var cancelDropDownClosing");
        sb.Append(CB1.ClientID);
        sb.Append(@"=false; function onDropDownClosing");
        sb.Append(CB1.ClientID);
        sb.Append(@"(){cancelDropDownClosing");
        sb.Append(CB1.ClientID);
        sb.Append(@"= false;}");
        sb.Append(@"function onCheckBoxClick");
        sb.Append(CB1.ClientID);
        sb.Append(@"(){var combo=$find('");
        sb.Append(CB1.ClientID);
        sb.Append(@"');var chkall=$get(combo.get_id() + '_Header_SelectAll');if(AllSelected");
        sb.Append(CB1.ClientID);
        sb.Append(@"()==true){chkall.checked=true;}else{chkall.checked=false;}var text='';var values='';var items=combo.get_items();");
        sb.Append(@"for(var i=0;i<items.get_count();i++){var item=items.getItem(i);var chk1=$get(combo.get_id()+'_i'+i+'_chk1');");
        sb.Append(@"if(chk1.checked){text+=item.get_text()+'");
        sb.Append(Delimiter);
        sb.Append("';values+=item.get_value()+'");
        sb.Append(Delimiter);
        sb.Append("';}}text=removeLastDelimiter(text);values=removeLastDelimiter(values);if(text.length>0){combo.set_text(text);}else{combo.set_text('');}}");
        sb.Append(@"function AnyOneSelected");
        sb.Append(CB1.ClientID);
        sb.Append(@"(){var combo=$find('");
        sb.Append(CB1.ClientID);
        sb.Append(@"');var items=combo.get_items();for(var i=0;i<items.get_count();i++){var item=items.getItem(i);");
        sb.Append(@"var chk1=$get(combo.get_id()+'_i'+i+'_chk1');if(chk1.checked){return true;}}return false;}");
        sb.Append(@"function AllSelected");
        sb.Append(CB1.ClientID);
        sb.Append(@"(){var combo=$find('");
        sb.Append(CB1.ClientID);
        sb.Append(@"');var items=combo.get_items();for(var i=0;i<items.get_count();i++){var item=items.getItem(i);");
        sb.Append(@"var chk1=$get(combo.get_id()+'_i'+i+'_chk1');if(chk1.checked==false){return false;}}return true;}");
        sb.Append(@"function SelectAllClick");
        sb.Append(CB1.ClientID);
        sb.Append(@"(chk){var selectAll=true; if(AnyOneSelected");
        sb.Append(CB1.ClientID);
        sb.Append(@"()==true)selectAll=true;if(AllSelected");
        sb.Append(CB1.ClientID);
        sb.Append(@"()==true)selectAll=false;var text='';var values='';var combo = $find('");
        sb.Append(CB1.ClientID);
        sb.Append(@"');var items=combo.get_items();for(var i=0;i<items.get_count();i++){var item=items.getItem(i);");
        sb.Append(@"var chk1=$get(combo.get_id()+'_i'+i+'_chk1');if(selectAll)chk1.checked=true;else chk1.checked=false;");
        sb.Append(@"if(chk1.checked){text += item.get_text()+'");
        sb.Append(Delimiter);
        sb.Append("';values+=item.get_value()+'");
        sb.Append(Delimiter);
        sb.Append("';}}text=removeLastDelimiter(text);values=removeLastDelimiter(values);if(text.length>0)combo.set_text(text);else ");
        sb.Append(@"combo.set_text('');}</script>");
        string js = sb.ToString();
        // Gets the executing web page
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Register the javascript on the page with the client id appended.
        page.ClientScript.RegisterClientScriptBlock(typeof(_Default), "DDC_" + CB1.ClientID, js);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create the javascript functions.
        if (!this.IsPostBack)
        {
            // Add the events
            this.CB1.OnClientDropDownClosed = "onDropDownClosing" + CB1.ClientID;
            (CB1.Header.FindControl("SelectAll") as CheckBox).Attributes["onclick"] = "SelectAllClick" + CB1.ClientID + "(this)";
            string[] itemsThietBi = { "MA5300", "MA5303", "SAM960", "M41", "IES5K", "IES1K", "T64G", "T160G", "S6506R", "MA5103", "MA5605" };
            CB1.DataSource = itemsThietBi;
            CB1.DataBind();
        }
    }

    protected void CB1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        (e.Item.FindControl("chk1") as CheckBox).Attributes["onclick"] = "onCheckBoxClick" + CB1.ClientID + "(this)";
    }

    public string GetCheckBoxValues()
    {
        StringBuilder sbValues = new StringBuilder();
        foreach (Telerik.Web.UI.RadComboBoxItem rcbItem in DDList.Items)
        {
            //If the box is checked return a value
            CheckBox cb = (CheckBox)rcbItem.FindControl("chk1");
            if (null != cb && cb.Checked)
            {
                sbValues.Append(rcbItem.Text);
                sbValues.Append(Delimiter);
            }
        }
        //Remove Trailing comma
        string str = sbValues.ToString();
        if (str.EndsWith(Delimiter))
            return str.Remove(sbValues.Length - 1, 1);
        return str;
    }

    public void SetCheckBoxValues(string csv)
    {
        // First clear all checks.
        foreach (RadComboBoxItem item in DDList.Items)
        {
            CheckBox chkbox = (CheckBox)item.FindControl("chk1");
            if (null != chkbox)
                chkbox.Checked = false;
        }

        // Find each item in the list and set the check and combo text value.
        string[] values = csv.Split(',');
        string text = string.Empty;
        for (int i = 0; i <= values.Length - 1; i++)
        {
            RadComboBoxItem item = DDList.FindItemByValue(values[i]);
            CheckBox chkbox = (CheckBox)item.FindControl("chk1");
            if (text.Equals(string.Empty))
                text = string.Format("{0}", item.Text);
            else
                text = string.Format("{0}{1} {2}", text, Delimiter, item.Text);
            chkbox.Checked = true;
        }
        DDList.Text = text;     // This doesn't do anything!
    }

How to use javascript to set select items?

Thanks,

4 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 11 Mar 2010, 03:21 PM
Hi hoc tro,

You can use the client-side select method of a RadComboBoxItem object to select it, e.g.
var item = comboBox.get_items().getItem(1);
item.select();

Regards,
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
Minh
Top achievements
Rank 1
answered on 11 Mar 2010, 05:29 PM
Hi Simon,
RadCombobox used checkbox select items, i use code javascript:
var item = comboBox.get_items().getItem(1);
item.select();

But not get select item, multi-item. Can you help me?
Brgs,
0
Simon
Telerik team
answered on 11 Mar 2010, 06:49 PM
Hello hoc tro,

As I am not completely sure what your requirement is, can you please provide more details about it?

All the best,
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
Megan Vee
Top achievements
Rank 1
answered on 27 May 2010, 04:35 PM
Using the code above (i can't find the original post with the zipped project, but i have it on my local machine), how can I add the RadComboBox AND ItemTemplate Checkbox to the page in c# (in the code behind) instead of using html/.net?  Please help!

Thank you,
Megan
Tags
ComboBox
Asked by
Minh
Top achievements
Rank 1
Answers by
Simon
Telerik team
Minh
Top achievements
Rank 1
Megan Vee
Top achievements
Rank 1
Share this question
or