954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Databind menu component from sql

Hi i have a table which i was using with a datagrid using hyperlink to maintain a simple menu to browse a internal site. but now i decided to create a horizontal menu with the Menu Component of asp.net, i know there is a different way how to do this, using sitemap for example. but i really want to keep my table providing all the path.

My table have the following columns
Menu
Id_Menu
Title
URL
Option_Fun

Which Option_Fun identify the parent for example
1, Google, www.google.com , General
2, Yahoo, www.yahoo.com , General

so in that case i know that under General i have 2 links. well my thing is can i generate a menu using as datasource my table?

Thanks any other suggestion i will appreciate.

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 

Do you mean?

nameOfTable.DataSource
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
 

yes something like that. i have not try it yet, but I am wonder how im going to set the sub menus and all that.

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 

Hi,

If you have submenus as well i dont think its possible to do what you want and use datasource(but im not sure).
Why not build your self the routine, that take out the table from the DB and populate the menu "manually".

IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
 

You're saying populate the menu manually thru a sitemap?

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 

Yea a sitemap or build a usercontrol that has subitems, or use others control that has item --> subitems...,
Its up to you to decide which control \ method to use.

IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
 

Ok, thanks for your help, I will be playing around with some of it. I let you know how i am doing.

Regards.

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 

Yea not a problem, if you need any help on the control you have decided to work with, do not be shy to ask.

Dont forget to close this therad, by flag it as solved.

IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
 

i think what you need is a self-referencing table, so the records will have two fields one of which is primary key and the other one is foreign key to its parent. by doing that you can have limitless hierarchy of links.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

I found what i wanted.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PopulateMenu();
            }
        }
        private void PopulateMenu()
        {
            DataSet ds = GetDataSetForMenu();
            foreach (DataRow parentItem in ds.Tables["MENUOPTS_T"].Rows)
            {
                MenuItem categoryItem = new MenuItem((string)parentItem["OPTION_FNCTN"]);
               
                EntertainmentMenu.Items.Add(categoryItem);
               
                foreach (DataRow childItem in parentItem.GetChildRows("Children"))
                {
                    MenuItem childrenItem = new MenuItem((string)childItem["OPTION_DESC"]);
                    childrenItem.NavigateUrl = ((string)childItem["OPTION_LINK"]);
                    categoryItem.ChildItems.Add(childrenItem);
                }
            }
        }
        private DataSet GetDataSetForMenu()
        {
            SqlConnection myConnection = new SqlConnection("Initial Catalog=;Data Source=;UID=;PWD=;");
            SqlDataAdapter adCat = new SqlDataAdapter("SELECT DISTINCT OPTION_FNCTN FROM MENUOPTS_T", myConnection);
            SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM EW_V_MENU", myConnection);

            DataSet ds = new DataSet();
            adCat.Fill(ds, "MENUOPTS_T");
            adProd.Fill(ds, "EW_V_MENU");
            ds.Relations.Add("Children",
               ds.Tables["MENUOPTS_T"].Columns["OPTION_FNCTN"],
               ds.Tables["EW_V_MENU"].Columns["OPTION_FNCTN"]);
            dg.DataSource = ds;
            dg.DataBind();
            return ds;
        }

Thanks all.

jbisono
Posting Pro in Training
442 posts since May 2009
Reputation Points: 71
Solved Threads: 59
 

mark as solved

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You