2009-11-11 137 views
-1

重复:
Hiding a link in asp.net隐藏在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文件如下 - >

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,因此我不能隐藏它。什么是替代这个?

回答

0

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

链接本身并不是一个control,所以它不会有自己的ID。它是属于LinkBar控件的链接列表的成员,并且LinkBar负责将它们呈现在屏幕上。

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

我建议你摆脱LinkBar,只需将控件添加到页面作为简单的HyperLink控件。如果你喜欢,可以在设计师那里做。然后就可以设置代码中的每个链路的可见度后面使用这些超链接的可见性属性,像这样:

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

耶 所以当我写 默认母版=(默认)this.Page.Master; masterPage.ctrlLinkBar.Visible = false; 在TimeEntryForm.aspx.cs的Page_Load中,它隐藏了整个控件。 如何隐藏控件的一个链接按钮? – Sophie 2009-11-11 01:03:05

+0

好酷生病尝试。 欢呼! – Sophie 2009-11-11 01:07:13