Dynamic Menu Creation...

3 posts, 0 answers
  1. Ranganath
    Ranganath avatar
    38 posts
    Member since:
    Jun 2008

    Posted 13 Jul 2008 Link to this post

    Anyone can help to Create Dynamic Menu’s Through Databases by using Telerik Menu control.


    Regards,
    Ranganath.S
  2. Kevin Babcock
    Kevin Babcock avatar
    189 posts
    Member since:
    Mar 2007

    Posted 13 Jul 2008 Link to this post

    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.

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicRadMenu.aspx.cs" Inherits="DynamicRadMenu" %> 
    2  
    3 <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    4  
    5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    6  
    7 <html xmlns="http://www.w3.org/1999/xhtml"
    8 <head runat="server"
    9     <title>Untitled Page</title> 
    10 </head> 
    11 <body> 
    12     <form id="form1" runat="server"
    13         <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />       
    14         <telerik:RadMenu ID="RadMenu1" runat="server" />         
    15     </form> 
    16 </body> 
    17 </html> 
    18  

    1 using System; 
    2 using System.Data; 
    3 using System.Data.SqlClient; 
    4  
    5 public partial class DynamicRadMenu : System.Web.UI.Page 
    6
    7     protected void Page_Load(object sender, EventArgs e) 
    8     { 
    9         var dataTable = this.GetData(); 
    10         foreach (DataRow row in dataTable.Rows) 
    11         { 
    12             var companyName = Convert.ToString(row["CompanyName"]); 
    13             var navigateUrl = String.Format("http://www.someurl.com/customers?id={0}", Convert.ToString(row["CustomerID"])); 
    14             var menuItem = new Telerik.Web.UI.RadMenuItem(companyName, navigateUrl); 
    15             RadMenu1.Items.Add(menuItem); 
    16         } 
    17     } 
    18  
    19     private DataTable GetData() 
    20     { 
    21         var data = new DataTable(); 
    22         using (SqlConnection con = new SqlConnection("Data Source=.\\SQLSERVER;Initial Catalog=Northwind;User ID=sa;Password=*******")) 
    23         { 
    24             var sql = "SELECT TOP 5 [CompanyName], [CustomerID] FROM [Customers];"
    25             using (SqlCommand cmd = new SqlCommand(sql, con)) 
    26             { 
    27                 cmd.CommandType = CommandType.Text; 
    28                 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
    29  
    30                 try 
    31                 { 
    32                     // Retrieve the records 
    33                     adapter.Fill(data); 
    34                 } 
    35                 catch (SqlException e) { } 
    36             } 
    37         }  
    38         return data; 
    39     } 
    40
    41  

    I hope this example helps.

    Sincerely,
    Kevin Babcock
  3. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 14 Jul 2008 Link to this post

    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
Back to Top