2009-11-11 134 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