利用menu实现后台管理用户界面

本文介绍如何使用ASP.NET中的Menu控件实现导航菜单,并通过母版页解决MenuItemClick事件仅触发一次的问题。示例代码展示了不同菜单项对应的不同内容页及验证逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

asp.net   menu控件,在普通页面只会执行一次MenuItemClick事件。如果用母版页,把menu放在页面左边,内容页

放在右面,很容易解决menu只响应一次的问题。

先建立母版页site1.master

前台代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    
    <form id="form1" runat="server">
        <div style="margin-top:20px;background-color:lightblue;">
            <asp:Label ID="Label1" runat="server" Width="90%" Text=" Manage user "></asp:Label>
        </div>
        <div style="float:left;margin-left:100px;margin-top:50px">
        <asp:Menu ID="Menu1" runat="server" StaticDisplayLevels="2"  OnMenuItemClick="Menu1_MenuItemClick"   >
            <Items >
                <asp:MenuItem Text="User Manage" Value="manage" Selectable="False">
                <asp:MenuItem Text="Add user" Value="add"></asp:MenuItem>
                <asp:MenuItem Text="Modify password" Value="modify"></asp:MenuItem>
                <asp:MenuItem Text="Permission" Value="mpermission"></asp:MenuItem>
                <asp:MenuItem Text="User void" Value="uservoid"></asp:MenuItem>
                </asp:MenuItem>
            </Items>
        </asp:Menu>
        </div>
         
      
    <div style="float:right;margin-right:700px;margin-top:100px">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>
后台c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            

            if (Menu1.SelectedValue == "add")
            {
                Response.Redirect("Default.aspx?rowdis=" + "add");
                return;
            }
            else
            {
                if (Menu1.SelectedValue == "modify")
                {
                    Response.Redirect("Default.aspx?rowdis=" + "modify");

                    return;
                }
                else
                {
                    if (Menu1.SelectedValue == "uservoid")
                    {
                        Response.Redirect("Default.aspx?rowdis=" + "uservoid");

                        return;
                    }
                    else
                    {
                        if (Menu1.SelectedValue == "mpermission")
                        {
                            Response.Redirect("Default.aspx?rowdis=" + "mpermission");

                            return;
                        }
                      

                      
                    }
                }
            }
        }
    }
}
默认一个主页,就是menu没有点击的时候显示的页面。比如是webform3.aspx,不执行任何代码

<%@ Page Title="主页" Language="C#" AutoEventWireup="true" MasterPageFile="~/Site1.Master" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1">
   
    </asp:Content>

下面是真正的内容页default.aspx,前台代码:

<%@ Page Title="主页" Language="C#" AutoEventWireup="true" MasterPageFile="~/Site1.Master" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1">
    <script  type="text/JavaScript">
        function checkForm() {
            var _uid = document.getElementById("<%=username.ClientID %>").value;
            var _upwd = document.getElementById("<%=password.ClientID %>").value;
            var _upwdSure = document.getElementById("<%=passwordconfirm.ClientID %>").value;

            if (_uid == "") {
                alert("username not empty!");
                return false;
            }

            if (_upwd != _upwdSure) {
                alert("twice passwords  are not match ");
                return false;

            }
            var reg = /^[a-zA-Z0-9]{6,8}$/g;
            if (!reg.test(_upwd)) {
                alert("Password length must be 6 -8 characters, can only be English words, numbers, for example: snsn2016, etc.");
                return false;
            }
            var reguser = /^[a-zA-Z]/;
            if (!reguser.test(_uid)) {
                alert("username can only be English words  for example: snsn, etc.");
                return false;
            }
        }

    </script>
    <div >
        <asp:Table ID="Table1" runat="server">
            <asp:TableRow ID="user" Visible="false">
                <asp:TableCell>
                    <asp:Label ID="Label2" runat="server">User Name:</asp:Label>
                    
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="username" runat="server" ></asp:TextBox>
                </asp:TableCell>
            </asp:TableRow>
              <asp:TableRow  ID="changepassword" Visible="false">
                <asp:TableCell>
                    <asp:Label ID="Label3" runat="server">Password:</asp:Label>
                    
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="password" runat="server"  ></asp:TextBox>
                </asp:TableCell>
                   
            </asp:TableRow>

                <asp:TableRow  ID="confchangepassword" Visible="false">
                <asp:TableCell>
                    <asp:Label ID="Label4" runat="server">Password Confirm:</asp:Label>
                    
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="passwordconfirm" runat="server"  ></asp:TextBox>
                </asp:TableCell>
               </asp:TableRow>
                 <asp:TableRow  ID="permission" Visible="false">
                <asp:TableCell>
                    <asp:Label ID="Label5" runat="server">Permission:</asp:Label>
                    
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList runat="server" ID="dlpermission">
                        <asp:ListItem Text="insert" Value="insert"></asp:ListItem>
                        <asp:ListItem Text="query" Value="query"></asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>

            </asp:TableRow>
             <asp:TableRow  ID="uservoid" Visible="false">
                <asp:TableCell>
                    <asp:Label ID="Label6" runat="server">User Void:</asp:Label>
                    
                </asp:TableCell>
                <asp:TableCell>
                    <asp:CheckBox ID="usvoid" runat="server" />
                </asp:TableCell>

            </asp:TableRow>

        </asp:Table>
     <asp:Button ID="submit" runat="server" Text="Submit" Visible="false" OnClick="submit_Click"  OnClientClick="return checkForm()" />
    <br />
     <asp:label ID="display" runat="server" text=" "></asp:label>
      </div>
    </asp:Content>




后台c#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string tbrow;
            tbrow = Request.QueryString["rowdis"].ToString();
            user.Visible = true;
            submit.Visible = true;

            if (!Page.IsPostBack)
            {
                if (tbrow == "add")
                {
                    changepassword.Visible = true;
                    confchangepassword.Visible = true;
                    permission.Visible = true;
                    uservoid.Visible = true;
                    inittextbox();
                    return;
                }
                else
                {
                    if (tbrow == "modify")
                    {
                        changepassword.Visible = true;
                        confchangepassword.Visible = true;
                        permission.Visible = false;
                        uservoid.Visible = false;
                        inittextbox();
                        return;
                    }
                    else
                    {
                        if (tbrow == "uservoid")
                        {
                            uservoid.Visible = true;
                            changepassword.Visible = false;
                            confchangepassword.Visible = false;
                            permission.Visible = false;
                            inittextbox();
                            return;
                        }
                        else
                        {
                            if (tbrow == "mpermission")
                            {
                                uservoid.Visible = false;
                                changepassword.Visible = false;
                                confchangepassword.Visible = false;
                                permission.Visible = true;
                                inittextbox();
                                return;
                            }
                            else
                                display.Text = "select error";
                            return;
                        }
                    }
                }
            }
           
        }
        protected void submit_Click(object sender, EventArgs e)
        {
            if (username.Text.Length > 0)
            {
                display.Text = "has name";
            }
            else
            {
                display.Text = "username empty";
            }
        }
        private void inittextbox()
        {
            username.Text = "";
            password.Text = "";
            passwordconfirm.Text = "";
        }
    }
}

后面在加一个抽象类,用来做页面传参的加密工作code.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;


namespace WebApplication1
{
    public abstract class code
    {

        public static string key = "20161030", iv = "01234567";
        public static string Encrypt(string sourceString)
        {
            try
            {
                byte[] btKey = Encoding.UTF8.GetBytes(key);

                byte[] btIV = Encoding.UTF8.GetBytes(iv);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                    try
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);

                            cs.FlushFinalBlock();
                        }

                        return Convert.ToBase64String(ms.ToArray());
                    }
                    catch
                    {
                        return sourceString;
                    }
                }
            }
            catch { }

            return "DES error";
        }
        public static string Decrypt(string encryptedString)
        {
            byte[] btKey = Encoding.UTF8.GetBytes(key);

            byte[] btIV = Encoding.UTF8.GetBytes(iv);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            using (MemoryStream ms = new MemoryStream())
            {
                byte[] inData = Convert.FromBase64String(encryptedString);
                try
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);

                        cs.FlushFinalBlock();
                    }

                    return Encoding.UTF8.GetString(ms.ToArray());
                }
                catch
                {
                    return encryptedString;
                }
            }

        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值