在asp.net中隐藏链接

时间:2009-11-11 00:43:41

标签: asp.net

  

重复:
  Hiding a link in asp.net


您好 这是主页的cs文件......

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");
        }
    }
}

我需要隐藏链接时间过滤器。 LinkBar的cs文件是

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>
");
        }
    }
}

我该怎么做?我更改了master.designer.cs文件,如下所示 - &gt;

 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;
    }

但链接仍然没有出现在母版页的设计视图中,因此我找不到ID,因此我无法隐藏它。有什么替代方案?

1 个答案:

答案 0 :(得分:0)

我假设您正在谈论隐藏TimeEntryForm.aspx的链接,并且您可能只想在有限的情况下这样做(这就是为什么您不想只省略该行)。< / p>

链接实际上本身并不是control,因此它没有自己的ID。它是属于LinkBar控件的链接列表的成员,LinkBar负责将它们呈现在屏幕上。

当您在运行时将这些链接添加到LinkBar时,它们不会显示在Visual Studio的设计视图预览中 - 它只会在您在浏览器中查看页面时显示。

我建议你摆脱LinkBar,只需将控件添加到页面中作为简单的HyperLink控件。如果您愿意,请在设计师中执行此操作。然后,您可以使用这些超链接上的Visible属性设置后面代码中每个链接的可见性,例如:

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