简体   繁体   中英

Access to Site.Master objects from an asp.net file

I have a asp page that is using Site.Master.

I put a link in Site.Master, and master included default.aspx. How can I access link attribute in asp file?

in Site.Master :

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Register.aspx" 
                    Target="_blank">Register</asp:HyperLink>

The only way to do this is to do something along the lines of:

HyperLink link =  this.Master.FindControl("id_of_the_control_you_are_looking_for") as HyperLink;

But note thet FindControl is not recursive, so you'll need to first find the control that holds the hyperlink you are looking for and then do another FindControl on this container using the id of the hyperlink you need.

The first thing you need to do is add a MasterType directive to your content page:

<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Namespace.Default" %>
<%@ MasterType VirtualPath="~/Site.Master" %>

Now you can access every public item from your MasterPage in your content page.

Let's say your MasterPage has this in its codebehind:

public partial class Site : System.Web.UI.MasterPage
{
    public string Name { get; set; }
}

You can then do this in your content page:

public partial class Default
{
    public void Page_Load(object sender, EventArgs e)
    {
        Master.Name = "Bobby";
    }
}

I'm not sure what you mean with "link attribute" in your question, but if you mean an HyperLink, here's how you can do this:

public partial class Site : System.Web.UI.MasterPage
{
    public HyperLink SharedHyperLink { get { return myHyperLink; } }
}

public partial class Default
{
    public void Page_Load(object sender, EventArgs e)
    {
        Master.SharedHyperLink.NavigateUrl = "/Contact.aspx";
    }
}

Let's assume that you have a link somewhere inside your Master page:

<asp:LinkButton ID="mylink" runat="server" Text="OK" CssClass="test" />

Then in the code behind of the child page you could fetch it like this:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var link = FindControlRecursive(Master, "mylink") as LinkButton;
        if (link != null)
        {
            var css = link.CssClass;
            // TODO: do something with the link
        }
    }

    private static Control FindControlRecursive(Control control, string id)
    {
        if (control.ID == id)
        {
            return control;
        }

        foreach (Control ctrl in control.Controls)
        {
            var foundCtrl = FindControlRecursive(ctrl, id);
            if (foundCtrl != null)
            {
                return foundCtrl;
            }
        }
        return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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