Navigation

Saturday, 1 December 2012

How to use DropDownControl with Check box and Multi-Select

Code On DropDownControl.ascx(Install Telerik Demo Version)
(Instal Trial Vesion of Telerik from this  url:http://demos.telerik.com/aspnet-ajax/)

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DropDownControl.ascx.cs" Inherits="college_CollegeProfileModule_DropDownControl" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<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;
    }
</script>

<div>
    <table>
        <tr>
            <td style="height: 43px">
                <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>
            </td>
        </tr>
    </table>
</div>




Code On DropDownControl.ascx.cs Page
(Instal Trial Vesion of Telerik from this  url:http://demos.telerik.com/aspnet-ajax/)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
public partial class college_CollegeProfileModule_DropDownControl : System.Web.UI.UserControl
{
    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)";
        }
    }
    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(college_CollegeProfileModule_DropDownControl), "DDC_" + CB1.ClientID, js);
    }

    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!
    }
}



Code to Bind DropDownControl
       //GetDataSet() is a function to bind the data from Database You Can Use Your Function to Bind
       //DDCity is the name of DropDownControl
      VijayDAL vd = new VijayDAL
        dataset ds = new dataset();

       ds = vd.GetDataSet("Usp_GETCity");
                if (ds != null)
                {
                    DDCity.DDList.Items.Clear();
                    DDCity.DDList.DataSource = ds.Tables[0].DefaultView;
                    DDCity.DDList.DataTextField = "CITY_NAME";
                    DDCity.DDList.DataValueField = "CITY_NAME";
                    DDCity.DDList.DataBind();

               
                }



code to Get Value of DropDownControl

DDCity.GetCheckBoxValues().ToString()

Code to Set Value of DropDownControl(use Comma(,))

 DDCity.SetCheckBoxValues(Delhi,Mumbai,Kolkata,Channai);


No comments:

Post a Comment