Answered by:
Accessing Checked Items in RadMenu in Code Behind

Question
-
User-971051533 posted
I have a RadMenu that I have added checkboxes to the child menu items and the checkboxes are then bound to a value. I need to be able to allow the user to check items in the menu and then when a submit button is clicked it will filter a RadGrid. I'm not sure how to access the checked values in the RaadMenu in submit button click event? I'll paste my code below.
<telerik:RadMenu ID="handsetMenu" runat="server" OnClientItemClicking="DisableRootClick" OnItemClick="handsetMenu_ItemClick"> <Items> </Items> </telerik:RadMenu> foreach (var cat in CatProdList) { var itemCategory = new RadMenuItem(cat.Category.Name); handsetMenu.Items.Add(itemCategory); var option = cat.ProdAttributesTuple.GroupBy(a => a.Item2.Attribute.Value).ToList(); foreach (var attr in option) { //itemCategory.Items.Add(new RadMenuItem(attr.Key)); var item = new RadMenuItem(attr.Key); itemCategory.Items.Add(item); CustomContentTemplate template = new CustomContentTemplate(); item.ContentTemplate = new CustomContentTemplate(); template.InstantiateIn(item); item.DataBind(); } } class CustomContentTemplate : ITemplate { public void InstantiateIn(Control container) { CheckBox cb = new CheckBox(); cb.DataBinding += new EventHandler(cb_DataBinding); container.Controls.Add(cb); } private void cb_DataBinding(object sender, EventArgs e) { CheckBox target = (CheckBox)sender; RadMenuItem item = (RadMenuItem)target.BindingContainer; target.Text = item.Text; } } protected void btnSubmit_Click(object sender, EventArgs e) { }
Friday, August 21, 2015 11:17 AM
Answers
-
User-271186128 posted
Hi hollyquinn,
With reference to the following link, it seems 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.
http://www.telerik.com/forums/how-to-get-access-to-checkbox-within-radmenu
Besides, I suggest you could refer to the following code to add templates to RadMenu at runtime.
protected override void OnInit(EventArgs e) { RadMenu1.ItemTemplate = new TextBoxTemplate(); base.OnInit(e); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { RadMenu1.Items.Add(new RadMenuItem("MenuItem1")); RadMenu1.Items.Add(new RadMenuItem("MenuItem2")); } RadMenu1.DataBind(); } class TextBoxTemplate : ITemplate { public void InstantiateIn(Control container) { Label label1 = new Label(); label1.ID = "ItemLabel"; label1.Text = "Text"; label1.Font.Size = 15; label1.Font.Bold = true; label1.DataBinding += new EventHandler(label1_DataBinding); container.Controls.Add(label1); } private void label1_DataBinding(object sender, EventArgs e) { Label target = (Label)sender; RadMenuItem item = (RadMenuItem)target.BindingContainer; string itemText = (string)DataBinder.Eval(item, "Text"); target.Text = itemText; } }
For more details, please see: http://docs.telerik.com/devtools/aspnet-ajax/controls/menu/templates/adding-templates-at-run-time
As indicated in my previous reply, this issue is related to the Telerik control, if you have any further question about it, I suggest you could post the question to Telerik Forums.
Best Regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 28, 2015 2:49 AM
All replies
-
User-271186128 posted
Hi Holly Quinn,
As for this issue, please refer to the following sample and try to use FindControl method to find the relevant CheckBox.
<telerik:RadButton runat="server" ID="button0" Text="UncheckCheckBox" OnClick="button0_Click"> </telerik:RadButton> <telerik:RadPanelBar ID="RadPanelBar0" runat="server"> <Items> <telerik:RadPanelItem Text="PanleItem1"> <HeaderTemplate> <asp:CheckBox runat="server" ID="checkBox1" Text="CheckBox1" Checked="true" /> </HeaderTemplate> <ContentTemplate> </ContentTemplate> </telerik:RadPanelItem> </Items> </telerik:RadPanelBar>
Code Behind:
protected void button0_Click(object sender, EventArgs e) { RadPanelItem item = RadPanelBar0.Items[0]; CheckBox CheckItem = item.Header.FindControl("checkBox1") as CheckBox; CheckItem.Checked = false; }
For more details, please see: http://docs.telerik.com/devtools/aspnet-ajax/controls/panelbar/templates/accessing-controls-inside-templates
Besides, since this issue is related to the Telerik control, if you have any further question about it, I suggest you could post the question to Telerik Forums.
Best Regards,
DillionSunday, August 23, 2015 10:01 PM -
User-971051533 posted
Hi Zhi. Thanks for your help. I'm not sure the RadMenu works the same way that the RadPanel does though. I am getting the error message:
An exception of type 'System.NullReferenceException' occurred in App_Web_umdevm1i.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
on the last line of my code here:
protected void btnSubmit_Click(object sender, EventArgs e) { RadMenuItem item = handsetMenu.Items[0]; CheckBox CheckItem = item.FindControl("cb") as CheckBox; CheckItem.Checked = false; }
I think it can't find the CheckBox template that I created in the code behind:
class CustomContentTemplate : ITemplate { public void InstantiateIn(Control container) { CheckBox cb = new CheckBox(); cb.Text = "Text"; cb.DataBinding += new EventHandler(cb_DataBinding); container.Controls.Add(cb); } private void cb_DataBinding(object sender, EventArgs e) { CheckBox target = (CheckBox)sender; RadMenuItem item = (RadMenuItem)target.BindingContainer; target.Text = item.Text; } }
Do you know what I'm doing wrong? If you need to see anymore code just let me know. Thanks.
Tuesday, August 25, 2015 9:42 AM -
User-271186128 posted
Hi hollyquinn,
With reference to the following link, it seems 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.
http://www.telerik.com/forums/how-to-get-access-to-checkbox-within-radmenu
Besides, I suggest you could refer to the following code to add templates to RadMenu at runtime.
protected override void OnInit(EventArgs e) { RadMenu1.ItemTemplate = new TextBoxTemplate(); base.OnInit(e); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { RadMenu1.Items.Add(new RadMenuItem("MenuItem1")); RadMenu1.Items.Add(new RadMenuItem("MenuItem2")); } RadMenu1.DataBind(); } class TextBoxTemplate : ITemplate { public void InstantiateIn(Control container) { Label label1 = new Label(); label1.ID = "ItemLabel"; label1.Text = "Text"; label1.Font.Size = 15; label1.Font.Bold = true; label1.DataBinding += new EventHandler(label1_DataBinding); container.Controls.Add(label1); } private void label1_DataBinding(object sender, EventArgs e) { Label target = (Label)sender; RadMenuItem item = (RadMenuItem)target.BindingContainer; string itemText = (string)DataBinder.Eval(item, "Text"); target.Text = itemText; } }
For more details, please see: http://docs.telerik.com/devtools/aspnet-ajax/controls/menu/templates/adding-templates-at-run-time
As indicated in my previous reply, this issue is related to the Telerik control, if you have any further question about it, I suggest you could post the question to Telerik Forums.
Best Regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 28, 2015 2:49 AM -
User-971051533 posted
Hi Dillion,
I did decide to go with the RadComboBox, but I can't get it to work either. I'm haivng a problem finding the RadComboBox inside the filter template. I've put in a ticket with Telerik and posted in their forums, they haven't responded to either one. I thank you for your help with this.
Friday, August 28, 2015 8:21 AM