简体   繁体   English

更改母版页上链接按钮上的文本?

[英]change text on linkbutton on masterpage?

Im looking to change the text on a linkbutton on a masterpage when a user clicks a button on a content page and the resulting action meets certain criteria.当用户单击内容页面上的按钮并且结果操作符合某些标准时,我希望更改母版页上链接按钮上的文本。 Is this do-able?这是可行的吗? I cant seem to access the masterpage controls via intellisense, which I suppose makes sense, but is there a way around it?我似乎无法通过智能感知访问母版页控件,我认为这是有道理的,但是有没有办法解决它?

thanks again再次感谢

You have to find the control in the master page using the FindControl method, like...您必须使用FindControl方法在母版页中找到控件,例如...

(ControlType)Master.FindControl("controlID")


((LinkButton)Master.FindControl("LinkButtonID")).Text = "New Text";

Add a property in the code behind of your master page like this:在母版页后面的代码中添加一个属性,如下所示:

public LinkButton LButton
{
    get { return lButton; }
    set { lButton = value; }
}

At the top of your.aspx page, add this directive with a virtual path to your master page:在 your.aspx 页面的顶部,添加此指令以及母版页的虚拟路径:

<%@ MasterType VirtualPath="~/Master.master" %>

Rebuild the solution and in the code behind of your.aspx, you are able to do this:重建解决方案并在 your.aspx 后面的代码中,您可以执行以下操作:

Master.LButton.Text = "foo bar";

EDIT: If you want the text to persist over other pages: When the certain criteria is met, you could instead set a session variable:编辑:如果您希望文本保留在其他页面上:当满足某些条件时,您可以设置一个 session 变量:

Session.Add("Link Button Text", "foobar");

In the Page_Load of the MasterPage:在 MasterPage 的 Page_Load 中:

if(Session["Link Button Text"] != null)
{
   lButton.Text = Session["Link Button Text"].ToString(); 
} 

Use the Master keyword.使用 Master 关键字。 So Master.control will access a control on the master page.所以 Master.control 将访问母版页上的控件。

To provide another alternative to the solutions given, you could also consider giving the master an interface:要提供给定解决方案的另一种替代方案,您还可以考虑为 master 提供一个接口:

public interface IMaster
{
   string LinkButtonText { get; set; }
}

Apply this interface to the code-behind of the master.将此接口应用于 master 的代码隐藏。 Then do in the page:然后在页面中执行:

((IMaster)Page.Master).LinkButtonText = "XYZ";

Also, if you have a custom base page class, you can shadow the Master property and return this interface instead, so you can directly reference it.此外,如果您有自定义基页 class,则可以隐藏 Master 属性并返回此接口,以便直接引用它。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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