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

How to get access to checkbox within RadMenu

4 Answers 292 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Tan PS
Top achievements
Rank 1
Tan PS asked on 16 Feb 2012, 07:59 AM
Hi Telerik Support,

I have created a list of checkbox controls within Radmenu from code-behind with one root node and a list of menu-items directly under this root node with 3 columns - see attachment.  

Test.aspx
<telerik:RadMenu runat="server" ID="RadMenu1" Skin="Vista"
   DataFieldID="ItemId" DataFieldParentID="ParentItemId"
    DataValueField="ItemId" DataTextField="Text" OnClientLoad="OnClientMenuLoaded">
    <DefaultGroupSettings RepeatColumns="3" RepeatDirection="Vertical" />
      </telerik:RadMenu>

Test.aspx.cs
namespace WebApplication1
{
    public partial class ListViewIn_Menu : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            RadMenu1.ItemTemplate = new TextBoxTemplate();
            base.OnInit(e);
        }
 
 
        protected void Page_Load(object sender, EventArgs e)
        {
               //Construct source list to bind to Menu
                List<MenuSelection> lstV = new List<MenuSelection>();
                MenuSelection lstItm;
                for(int i = 1; i<= 50 ; i++)
                {
                    if (lstV.Count == 0)
                    {
                        lstItm = new MenuSelection();
                        lstItm.ParentItemId = 0;
                        lstItm.ItemId = lstV.Count + 1;
                        lstItm.Checked = false;
                        lstItm.Text = "MenuLevel1";
                    }
                    else
                    {
                        lstItm = new MenuSelection();
                        lstItm.ItemId = lstV.Count + 1;
                        lstItm.ParentItemId = 1;
                        lstItm.Checked = GetSelectedList().Contains(lstItm.ItemId);
                        lstItm.Text = "MenuLevel2_" + (lstV.Count + 1).ToString();
                    }
                    lstV.Add(lstItm);
                }
 
                RadMenu1.DataSource = lstV;
                RadMenu1.DataBind();
 
 
 
        }
 
 
        private List<int> GetSelectedList()
        {
            //Get the selected checkbox text from hidden field (where checkbox is checked)
            List<int> selList = new List<int>();
            foreach (string itm in hidSelectedList.Value.Split(new char[] { '|' }))
            {
                if (!string.IsNullOrEmpty(itm))
                {
                    selList.Add(Convert.ToInt32(itm));
                }
            }
 
            return selList;
        }
 
    }
 
    public class MenuSelection
    {
        public MenuSelection()
        {
        }
 
        public int ItemId { get; set; }
 
        public int ParentItemId { get; set; }
 
        public string Text { get; set; }
 
        public bool Checked { get; set; }
    }
 
    class TextBoxTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            CheckBox cb = new CheckBox();
            cb.DataBinding += new EventHandler(cb_DataBinding);
            container.Controls.Add(cb);
        }
 
        void cb_DataBinding(object sender, EventArgs e)
        {
            CheckBox target = (CheckBox)sender;
            RadMenuItem item = (RadMenuItem)target.BindingContainer;
            MenuSelection ds = (MenuSelection)item.DataItem;
            target.Checked = ds.Checked;
            target.Text = ds.Text;
            target.ID = "ck";
            target.Attributes.Add("onclick", "OnMenuCheck_Click('" + ds.ItemId.ToString() + "')");
             
        }
    }

My issue is:- 
a) how can I use javascript to read all the text from checkbox (checked) and build a string store in hidden field so that I can access from code-behind.

b) When I check on the root node, all child-nodes will get checked. How to use javascript to read each checkbox in RadMenu?

c) I may add one hidden field together with checkbox in RadMenu then how to read this hidden field's value?

d) You have better way of presenting this design using other control?

I can use normal javascript to read them E.g. alert($get('RadMenu1_i0_i0_ck2').checked); But can I use the method provided by Telerik framework? I afraid this naming format may change in future by Telerik product upgrade. I have tried other methods provided in help file and from this forum with no help.

Anyone can help? Thanks in advanced.
PS

4 Answers, 1 is accepted

Sort by
0
Accepted
Kate
Telerik team
answered on 20 Feb 2012, 04:53 PM
Hello PS,

You can try using the RadCombobox control that has already a build-in property - CheckBoxes that you can set to true - that implements most of the functionality you describe. 

Kind regards,
Kate
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Cat Cheshire
Top achievements
Rank 1
answered on 20 Feb 2012, 05:42 PM
0
Tan PS
Top achievements
Rank 1
answered on 20 Feb 2012, 07:11 PM
Hi Cheshire,

Thanks for your answer. Finding item by Text is not a good idea as text could vary from internationalization setting etc. But there is no sample of searching item by control Id. Anyway, RadComboBox could be the best choice in this case.

Thanks again,
Tan
  
0
Tan PS
Top achievements
Rank 1
answered on 20 Feb 2012, 07:15 PM
Hi Kate, 

This is awesome. I like Telerik product - fast response and nice answer ;)
The sample shows only single column and I will look into multi-columns checkbox for 100 options and less & further style it later. 

You should implement rating on all the posts and you have my Max vote!

Thanks again,
PS
Tags
Menu
Asked by
Tan PS
Top achievements
Rank 1
Answers by
Kate
Telerik team
Cat Cheshire
Top achievements
Rank 1
Tan PS
Top achievements
Rank 1
Share this question
or