how to build dynamic menu with submenus

G

Guest

I've got a problem with asp.net i am trying to make a menu control. and have
searched the web for serveral controls but they don't work correctly.

I am pretty new to asp.net building. What am i looking for. i am looking for
code to make a dynamical menu with submenus The menu items are stored in a MS
SQL Table and when by loading the page the items will be created. i have made
a asp table. i want my menu on the left cell, in the middle cell i want to
change my pages to the menu item choosen.

This is what my page look like right now
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" runat="server" Height="72px" Width="944px">
<asp:TableRow>
<asp:TableCell>
<asp:linkbutton id="btnLogOut" style="Z-INDEX: 102; LEFT: 8px; TOP:
8px" runat="server" Width="80px"
Height="8px">Logout</asp:linkbutton> </asp:TableCell>
<asp:TableCell>
<asp:textbox id="txtUsername" style="Z-INDEX: 101; LEFT: 112px; TOP:
8px" runat="server" Width="144px"
Height="24px"></asp:textbox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<uc1:Menu id="Menu1" runat="server"></uc1:Menu>
</asp:TableCell>
<asp:TableCell>
<uc1:messages id="Messages1" runat="server"></uc1:messages>
</asp:TableCell>
<asp:TableCell>test</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>

so for an instance i want to change the messages to Sent items page how can
i do that?
It would be great if some one can give me some examples of making menus and
how to change the page.
and is it possible to do it all from the code behind?
 
M

Mr Newbie

You have already posted this question once. You only need one posting,
especially as you posted it just two hours ago, in addition, I answered your
post but you didnt respond.
 
K

Karl Seguin

Not 100% sure I understand what you are asking, so let me give you some
general advice in the hopes that it might point you to the right place.

Controls can be dynamically created and added to your page. That is, you
could have something like this:

<tr id="MenuContainer" runat="server"
</tr>

and in your codebehind do:

for each row as DataRow in MyDataTableOfMenus
dim link as string = cstr(row("link"))
dim name as string = cstr(row("name"))
dim td as HtmlTableCell = CreateMenuItem(link, name)
MenuContainer.COntrols.Add(td)
next


function CreateMenuItem(link as string, name as string)
dim td as new HtmlTableCell
dim a as new HtmlAnchor
a.InnerText = name
a.HRef = link
td.Controls.Add(a)
return td
end function


You can do this with user control, built in server controls (shown above) or
your own custom server controls..

Karl
 
G

Guest

Hi Karl it almost does but what do you do with sub menus?
how can you insert them in yoyr menu

kind regards

Paula
 
K

Karl Seguin

Well, in that case you have your dynamically created Td, which you must add
a new table, row and then ur sub menus..in that sense it could be recursive,
say somethin glike:

for each row as DataRow in MyDataTableOfMenus
dim link as string = cstr(row("link"))
dim name as string = cstr(row("name"))
dim childRows as DataRow() = row.GetChildRows("SubItems")
CreateMenuItem(MenuContainer, link, name, childRows)
next


function CreateMenuItem(parentControl as control, link as string, name as
string, childRows as DataRow())
dim td as new HtmlTableCell
dim a as new HtmlAnchor
a.InnerText = name
a.HRef = link
td.Controls.Add(a)
parentControl.Controls.Add(td)
if childRows.Length > 0 then 'we have sub items to add
dim table as new HtmlTable()
dim tr as new HtmlTableRow()
table.Controls.Add(tr)
td.Controls.Add(table)
foreach row as DataRow in childRows
dim link as string = cstr(row("link"))
dim name as string = cstr(row("name"))
dim subRows as DataRow() = row.GetChildRows("SubItems")
CreateMenuItem(tr, link, name, subRows )
next
end function


The code is a lot more complex. I made some assumptions, for example, I
assumed that sub menu items were referenced using a relationship and thus
made use of GetChildRows...but whatever you do, you should be able to see
how I've simply created a new table and row, added them to my dynamically
created cell, and repeated the process ad nauseum

Cheers,
karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top