Glemir Cambero
---Primero debe crear estas tablas para el manejo de menu
---- en estas estan las opciones que existen en todo el menu.
Создайте таблицу [dbo].[menu_opciones] (
[MenuId] INT IDENTITY (1, 1) NOT NULL,
[падремену] INT NOT NULL,
[хиджомену] NVARCHAR (10) NOT NULL,
[descripcion] NVARCHAR (50) NOT NULL,
[url] NVARCHAR (50) NOT NULL,
Ограничение [PK_YA_EXISTE_MENU] первичный ключ КЛАСТЕРИЗОВАН ([hijomenu] ASC)
);
-- Ан Эста табла insertas Лас-опции-дель-меню дие corresponden Аль Перфильева.
Создайте таблицу [dbo].[menu_perfiles_opciones] (
[codperfil] NVARCHAR (20) NOT NULL,
[хиджомену] NVARCHAR (10) NOT NULL
Ограничение [pk_perfil_y_opcion_ya_existen] первичный ключ КЛАСТЕРИЗОВАН ([codperfil] ASC, [hijomenu] ASC));
--- Акви Лос usuarios,.... Ан-Эсте Касо usaras Ла табла кы "тиэнэс" Донде insertas в Los usuarios. Дебе Эстар Ла Колумна "Перфиль "
Создайте таблицу [dbo].[usuarios_app] (
[usuario] NVARCHAR (80) NOT NULL,
[passw] NVARCHAR (20) NOT NULL,
[codperfil] NVARCHAR (20) NOT NULL,
Первичный ключ кластеризованного ([пользователь] АСК));
вставить в menu_opciones значения (0,'0.0','Inicio','~/Index.aspx'); --1
вставить в menu_opciones значения (0,'2.0','Menu administrador','#'); -- 2
вставить в menu_opciones значения (2,'2.1','Opcion de administrador','~/pagina2.aspx'); --3
вставить в menu_opciones значения (0,'4.0','Menu Usuario','#'); --4
вставка в ценности menu_opciones (4,'4.1','опцион-де-пользователь','~/сайт pagina2.страницы aspx'); --5
вставить в menu_perfiles_opciones значения ('ADMIN','0.0');
вставить в menu_perfiles_opciones значения ('USUARIO','0.0');
вставить в menu_perfiles_opciones значения ('ADMIN','2.0');
вставить в menu_perfiles_opciones значения ('ADMIN','2.1');
вставить в menu_perfiles_opciones значения ('ADMIN','4.0');
вставить в menu_perfiles_opciones значения ('ADMIN','4.1');
вставить в menu_perfiles_opciones значения ('USUARIO','4.0');
вставить в menu_perfiles_opciones значения ('USUARIO','4.0');
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Menú</title>
<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" BackColor="#E3EAEB" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="Medium" ForeColor="#666666" StaticSubMenuIndent="10px">
<DynamicHoverStyle BackColor="#666666" ForeColor="White" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicMenuStyle BackColor="#E3EAEB" />
<DynamicSelectedStyle BackColor="#1C5E55" />
<StaticHoverStyle BackColor="#666666" ForeColor="White" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<StaticSelectedStyle BackColor="#1C5E55" />
</div>
</form>
y codido
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.TraeOpcionesDeMenu(0);
CargaMenu(dt, 0, null);
}
}
private DataTable TraeOpcionesDeMenu(int parentMenuId)
{
string query = "SELECT *FROM menu_perfiles_opciones as mpo,menu_opciones as mo,usuarios_app as ua where MPO.hijomenu = MO. hijomenu and ua.codperfil = mpo.codperfil and mo.padremenu = @MenuIdPadre and ua.usuario=@User";
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; // ConnectionString sera el nombre de tu conexion.
using (SqlConnection con = new SqlConnection(constr))
{
DataTable dt = new DataTable();
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@MenuIdPadre", parentMenuId);
cmd.Parameters.AddWithValue("@User", Page.User.Identity.Name);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
private void CargaMenu(DataTable dt, int parentMenuId, MenuItem parentMenuItem)
{
string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
foreach (DataRow row in dt.Rows)
{
MenuItem menuItem = new MenuItem
{
Value = row["MenuId"].ToString(),
Text = row["descripcion"].ToString(),
NavigateUrl = row["url"].ToString(),
Selected = row["url"].ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
};
if (parentMenuId == 0)
{
Menu1.Items.Add(menuItem);
DataTable dtChild = this.TraeOpcionesDeMenu(int.Parse(menuItem.Value));
CargaMenu(dtChild, int.Parse(menuItem.Value), menuItem);
}
else
{
parentMenuItem.ChildItems.Add(menuItem);
}
}
}
}