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

Dynamic Menu Creation...

2 Answers 151 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Ranganath
Top achievements
Rank 1
Ranganath asked on 13 Jul 2008, 11:45 AM
Anyone can help to Create Dynamic Menu’s Through Databases by using Telerik Menu control.


Regards,
Ranganath.S

2 Answers, 1 is accepted

Sort by
0
Kevin Babcock
Top achievements
Rank 1
answered on 14 Jul 2008, 12:21 AM
Hello Ranganath,

Here is an example of connecting to the database (in this case, Northwind) and creating dynamic RadMenuItems to add to a RadMenu. Keep in mind that this is just a simple example and you would want to use your own data access classes to retrieve relevant data from your tables.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicRadMenu.aspx.cs" Inherits="DynamicRadMenu" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!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> 
</head> 
<body> 
    <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />       
        <telerik:RadMenu ID="RadMenu1" runat="server" />         
    </form> 
</body> 
</html> 
 

using System; 
using System.Data; 
using System.Data.SqlClient; 
 
public partial class DynamicRadMenu : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        var dataTable = this.GetData(); 
        foreach (DataRow row in dataTable.Rows) 
        { 
            var companyName = Convert.ToString(row["CompanyName"]); 
            var navigateUrl = String.Format("http://www.someurl.com/customers?id={0}", Convert.ToString(row["CustomerID"])); 
            var menuItem = new Telerik.Web.UI.RadMenuItem(companyName, navigateUrl); 
            RadMenu1.Items.Add(menuItem); 
        } 
    } 
 
    private DataTable GetData() 
    { 
        var data = new DataTable(); 
        using (SqlConnection con = new SqlConnection("Data Source=.\\SQLSERVER;Initial Catalog=Northwind;User ID=sa;Password=*******")) 
        { 
            var sql = "SELECT TOP 5 [CompanyName], [CustomerID] FROM [Customers];"
            using (SqlCommand cmd = new SqlCommand(sql, con)) 
            { 
                cmd.CommandType = CommandType.Text; 
                SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
 
                try 
                { 
                    // Retrieve the records 
                    adapter.Fill(data); 
                } 
                catch (SqlException e) { } 
            } 
        }  
        return data; 
    } 
 

I hope this example helps.

Sincerely,
Kevin Babcock
0
Veselin Vasilev
Telerik team
answered on 14 Jul 2008, 07:14 AM
Hi Ranganath,

In addition, you can download and watch the RadMenu for ASP.NET AJAX – Data Binding episode of our training tutorial.

Kind regards,
Veskoni
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Menu
Asked by
Ranganath
Top achievements
Rank 1
Answers by
Kevin Babcock
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or