收录日期:2019/10/18 22:07:20 时间:2009-11-11 00:55:05 标签:asp.net

Duplicate:
Hiding a link in asp.net


Hi this is the cs file of the masterpage...

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LevoContactManagement
{
    public partial class Default : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BasePage page = (BasePage)Page;

            if (page.CurrentUser != null)
            {
                lblCurrentUser.Text = "<strong>" + page.CurrentUser.FullName + "</strong> - " + page.CurrentUser.CompanyName;

                if ((Session["CCFUser"] != null) && (bool.Parse(Session["CCFUser"].ToString()) == true))
                {
                    ctrlLinkBar.AddLink("Issues Management", "AllIssues.aspx");
                }
                else
                {
                    if (true) ctrlLinkBar.AddLink("Home", "Default.aspx");
                    if (page.CurrentUser.Permissions.Issues()) ctrlLinkBar.AddLink("Issues Management", "AllIssues.aspx");
                    if (page.CurrentUser.Permissions.Time()) ctrlLinkBar.AddLink( "Time Management", "TimeEntryForm.aspx");
                    if (page.CurrentUser.Permissions.Time()) ctrlLinkBar.AddLink("Time Filter", "TimeFilter.aspx");
                    if (page.CurrentUser.Permissions.SVN() && !(this.Page is _Default)) ctrlLinkBar.AddLink("SVN", "SVN.aspx");
                    if (true) ctrlLinkBar.AddLink("Profile", "ChangePassword.aspx");
                    if (page.CurrentUser.Permissions.Administration()) ctrlLinkBar.AddLink( "Administration", "Administration.aspx");
                }

            }
            else lnkLogout.Visible = false;
        }
        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");
        }
    }
}

i need to make the link Time Filter hidden. the cs file of LinkBar is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebControlLib
{
    [ToolboxData("<{0}:LinkBar runat=server></{0}:LinkBar>")]
    public class LinkBar : WebControl
    {
        struct Link
        {
            public string Title;
            public string URL;

            public override string ToString()
            {
                return "<a href='" + URL + "'>" + Title + "</a>";
            }
        }

        private bool m_bIsVertical = false;
        private List<Link> m_Links = new List<Link>();

        public bool IsVertical
        {
            get
            {
                return m_bIsVertical;
            }
            set
            {
                m_bIsVertical = value;
            }
        }

        public void Clear()
        {
            m_Links.Clear();
        }
        public void AddLink(string Title, string URL)
        {
            Link lnk = new Link();

            lnk.Title = Title;
            lnk.URL = URL;

            m_Links.Add(lnk);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            List<string> items = new List<string>();

            foreach (Link lnk in m_Links)
                items.Add(lnk.ToString());

            string sep = IsVertical ? "</td></tr><tr><td>" : " | ";

            output.Write(
@"
<table width='100%' class='linkBar'>
    <tr>
        <td>" + string.Join(sep, items.ToArray()) + @"</td>
    </tr>
</table>
");
        }
    }
}

how do i go about it? i changed the master.designer.cs file as follows-->

 public partial class Default {
        protected System.Web.UI.HtmlControls.HtmlForm form1;
        protected System.Web.UI.WebControls.Label lblCurrentUser;
        protected System.Web.UI.WebControls.LinkButton lnkLogout;
        public WebControlLib.LinkBar ctrlLinkBar;
        public System.Web.UI.WebControls.ContentPlaceHolder LeftNav;
        protected System.Web.UI.WebControls.ContentPlaceHolder ContentPlaceHolder1;
        protected System.Web.UI.WebControls.ContentPlaceHolder BodyContent;
    }

but the link still does not appear on the Design view of the masterpage, hence i cant find the id, therefore i cant hide it. What is an alternative to this?

I assume that you're talking about hiding the link to TimeEntryForm.aspx, and that you probably want to do this in only limited circumstances (which is why you don't want to just omit the line).

The link isn't actually in itself a control, so it won't have its own ID. It's a member of the List of links that belongs to the LinkBar control, and the LinkBar takes care of rendering them to the screen.

As you're adding these links to the LinkBar at run time, they won't display in the design view preview in Visual Studio - it will only display when you view the page in a browser.

I'd suggest that you get rid of the LinkBar, and just add the controls to the page as simple HyperLink controls. If you like, do this in the designer. Then you can set the visibility of each link in the code behind using the Visible property on those hyperlinks, like such:

hlTimeLink.Visible = page.CurrentUser.Permissions.Time();