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 t开发者_运维技巧o 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...
(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:
<%@ MasterType VirtualPath="~/Master.master" %>
Rebuild the solution and in the code behind of your .aspx, you are able to do this:
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.Add("Link Button Text", "foobar");
In the Page_Load of the MasterPage:
if(Session["Link Button Text"] != null)
{
lButton.Text = Session["Link Button Text"].ToString();
}
Use the Master keyword. So Master.control will access a control on the master page.
To provide another alternative to the solutions given, you could also consider giving the master an interface:
public interface IMaster
{
string LinkButtonText { get; set; }
}
Apply this interface to the code-behind of the 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.
HTH.
精彩评论