简体   繁体   English

从菜单中删除特定项目并通过asp.net中的内容页面替换新菜单?

[英]Removing particular items from menu and replacing the new menu through content page in asp.net?

I have a menu and label in my master page which i want to update depending on the type of user logged in. 我的母版页中有一个菜单和标签,我想根据登录的用户类型进行更新。

Firstly am removing few MenuItems from the menu that is working fine but its not showing up in the master page. 首先,从正常工作的菜单中删除一些MenuItems,但该菜单项未显示在母版页中。 Instead the old menu is only seen with all the menu items for limited user also. 取而代之的是,旧菜单仅与有限用户的所有菜单项一起显示。 When i debug the label text shows what i have set but when page loads its not updating too. 当我调试时,标签文本会显示我已设置的内容,但在页面加载时不会更新。

Am using the following code. 我正在使用以下代码。

Label lbWelcomeMessage = new Label();
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("CAMenu").Visible = false;
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string userName = txtUsername.Text;
        string password = txtPassword.Text;
        Common common = new Common();
        DataTable tab = new DataTable();
        tab= common.GetUserDetails(userName);
        string firstName = string.Empty;
        string userPassword = string.Empty;
        string RoleID=string.Empty;

        if (tab.Rows.Count == 1)
        {
            firstName = tab.Rows[0][2].ToString();
            userPassword = tab.Rows[0][4].ToString();
            RoleID = tab.Rows[0][5].ToString();
        }
        if (userPassword == password)
        {
            if (RoleID != "1")
            {
                Menu CAMenu = new Menu();
                CAMenu = (Menu)Master.FindControl("CAMenu");
                int count = CAMenu.Items.Count;

                for (int i = 3; i > 0; i--)
                {
                    string text = CAMenu.Items[i - 1].Text;
                    CAMenu.Items.RemoveAt(i - 1);
                }

                lbWelcomeMessage = (Label)Master.FindControl("lbLoginMessage");
                lbWelcomeMessage.Text = "Welcome"+" "+ firstName;
                ((SiteMaster)Page.Master).MyText = lbWelcomeMessage.Text;
                Response.Redirect("AdHocSMS.aspx");
            }
            else
            {
                lbWelcomeMessage = (Label)Master.FindControl("lbLoginMessage");
                lbWelcomeMessage.Text = lbWelcomeMessage.Text+" "+firstName ;
                Response.Redirect("NewTemplate.aspx");
            }

        }
    }

I assume you have written this code in your login.aspx. 我假设您已经在login.aspx中编写了此代码。 When login button is clicked then it takes you to another page and all the life cycle of page is run again. 单击登录按钮后,它将带您到另一个页面,并且该页面的所有生命周期都将再次运行。 and master page contents are reset. 和母版页内容被重置。

The solution to this problem could be. 解决这个问题的方法可能是。 Move this logic to your master page code like 将此逻辑移至您的母版页代码,例如

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (Session.Count == 0 || Session["Username"] == null)
            Response.Redirect("~/Login.aspx", true);
        CheckRole();
    }
    public void CheckRole()
    {
        if (System.Web.HttpContext.Current.Session.Count > 0)
        {
            tab= common.GetUserDetails(Session["Username"]);
            if (tab.Rows.Count == 1)
         {
                 firstName = tab.Rows[0][2].ToString();
                 userPassword = tab.Rows[0][4].ToString();
                 RoleID = tab.Rows[0][5].ToString();
        }

           if (RoleID != "1")
            {
                 Menu CAMenu = new Menu();
                 int count = CAMenu.Items.Count;

                 for (int i = 3; i > 0; i--)
                  {
                    string text = CAMenu.Items[i - 1].Text;
                    CAMenu.Items.RemoveAt(i - 1);
                  }

    //your label logic
            lbWelcomeMessage.Text = "Welcome"+" "+ firstName;
            ((SiteMaster)Page.Master).MyText = lbWelcomeMessage.Text;
            Response.Redirect("AdHocSMS.aspx");
        }
        else
        {
    //Logic
            Response.Redirect("NewTemplate.aspx");
        }
    }
    else
    {
        Session.Abandon();
        Response.Redirect("~/Login.aspx", true);
    }
}

You have to put a UserId or Username in Session this is the one disadvantage but for every page you dont have to worry about anything. 您必须在会话中放置一个UserId或Username,这是一个缺点,但是对于每个页面,您都不必担心任何事情。

for life cycle read this article http://msdn.microsoft.com/en-us/library/ms178472.aspx 有关生命周期,请阅读此文章http://msdn.microsoft.com/zh-cn/library/ms178472.aspx

let me know if it solves or not. 让我知道它是否可以解决。

What i did is something like this 我所做的就是这样

In Site.Mater:- 在Site.Mater中:-

  public void CheckRole()
    {
        try
        {
            if (System.Web.HttpContext.Current.Session.Count > 0)
            {
                string firstName = string.Empty;
                // string userPassword = string.Empty;
                string RoleID = string.Empty;
                Common common = new Common();
                DataTable tab = new DataTable();
                string userName = (string)Session["UserName"];
                User user = new User(userName);
                tab = user.GetUserDetails(userName);

                if (tab.Rows.Count == 1)
                {
                    firstName = tab.Rows[0][1].ToString();
                    RoleID = tab.Rows[0][3].ToString();
                }

                if (RoleID != "1")
                {
                    int count = CAMenu.Items.Count;
                    if (count == 5)
                    {
                        for (int menuCount = 3; menuCount > 0; menuCount--)
                        {
                            string text = CAMenu.Items[menuCount - 1].Text;
                            CAMenu.Items.RemoveAt(menuCount - 1);
                        }
                    }
                    lbLoginMessage.Text = "Welcome," + " " + firstName;
                    loginStatus.Visible = true;
                }
                else
                {
                    lbLoginMessage.Text = "Welcome," + " " + firstName;
                    loginStatus.Visible = true;
                }

            }
            else
            {
                Session.Abandon();
                Response.Redirect("~/Login.aspx", true);
            }
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.SiteMaster.CheckRole()", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }

In Login.Apsx:- 在Login.Apsx中:-

 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Master.FindControl("CAMenu").Visible = false;
            Master.FindControl("loginStatus").Visible = false;
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.Login.btnLogin_Click(object sender, EventArgs e)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }
    protected void LoadMessageBox(string MessageID)
    {
        try
        {
            messages = new GUIMessages();
            popupExtend = new ModalPopupExtender();
            lbMessage = (Label)Master.FindControl("label5");
            lbMessage.Text = messages.GetGUIMessage(GUIModule.Login, MessageID);
            popupExtend = (ModalPopupExtender)Master.FindControl("popupExtender");
            popupExtend.Show();
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.Login.LoadMessageBox(string MessageID)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string userName = txtUsername.Text;
            string password = txtPassword.Text;

            if (userName == string.Empty && password == string.Empty)
            {
                LoadMessageBox("5");
                txtUsername.Focus();


                return;
            }
            if (userName == string.Empty)
            {
                LoadMessageBox("1");
                txtUsername.Focus();
                return;
            }
            else if (password == string.Empty)
            {
                LoadMessageBox("3");

                txtPassword.Focus();
                return;
            }

            User user = new User(userName);
            DataTable tab = new DataTable();
            tab = user.GetUserDetails(userName);
            string firstName = string.Empty;
            string userPassword = string.Empty;
            string RoleID = string.Empty;
            string userID = string.Empty;
            Session["UserName"] = userName;
            if (tab.Rows.Count == 0)
            {
                LoadMessageBox("6");
                txtPassword.Text = string.Empty;
                txtUsername.Text = string.Empty;
                txtUsername.Focus();
                return;
            }

            if (tab.Rows.Count == 1)
            {
                userID = tab.Rows[0][0].ToString();
                firstName = tab.Rows[0][1].ToString();
                userPassword = tab.Rows[0][2].ToString();
                RoleID = tab.Rows[0][3].ToString();
                Session["UserID"] = userID;
            }
            //if (firstName != userName)
            //{
            //    LoadMessageBox("2");
            //    txtUsername.Focus();
            //    return;
            //}
            //else
            {
                if (userPassword == password)
                {
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    LoadMessageBox("4");
                    txtPassword.Focus();
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.Login.btnLogin_Click(object sender, EventArgs e)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM