After exposing a public property in Top.Master it can be accessed in any child page that has a master type reference on its page.
How can the same properties be accessed from a nested page?
I tried to cascade the properties down the heirarchy but the child page errors when trying to access it.
I would prefer to access the exposed top.master property directly from the nested content page but am unsure of a good way to do this.
TOP.MASTER
<asp:Label ID="lblMsg" ClientIDMode="Static" runat="Server" />
TOP.MASTER.VB
Partial Public Class TopMaster
Inherits MasterPage
Public Property Msg As String
Get
Return lblMsg.Text
End Get
Set(value As String)
lblMsg.Text = value
End Set
End Property
End Class
CHILD.MASTER
<%# MasterType VirtualPath="~/Top.Master" %>
CHILD.MASTER.VB
Master.Msg = "Success"
CHILD.PAGE
<%# MasterType VirtualPath="~/Child.Master" %>
CHILD.PAGE.VB
Master.Master.Msg = "Success"
In your child.master class you can create a Msg property that would proxy the top master Msg property
You can add the following code in child.master.vb
Public Property Msg As String
Get
Return Master.Msg
End Get
Set(value As String)
Master.Msg = value
End Set
End Property
then in your child.page.vb you can access this property doing
Master.Msg = "Success"
Related
i have public property in user control and i need access to this property from HTML for parent for this user control
Public Property ActorID As Integer
Set(value As Integer)
ViewState("ActorID") = value
End Set
Get
Return ViewState("ActorID")
End Get
End Property
<uc3:Hos_Applicants ID="Hos_Applicants1" runat="server" />
<uc3:Hos_Applicants ID="Hos_Applicants1" runat="server" ActorID="2" />
you can access to property and set value in user control from parent asp page
We have ascx user controls written in VB.NET for an ASP.NET 4.0 project. The built-in VS page validator always displays the message from the question for all custom properties of our user control.
For instance, here is the beginning of the code of one of our controls:
<%# Control Language="VB" ClassName="PicView" %>
<%# Import Namespace="System.Drawing" %>
<script runat="server">
Public ImageUrl As String
When we try to use this control using code like this
<%# Register TagPrefix="foo" TagName="PicView" src="~/ascx/PicView.ascx" %>
<foo:PicView ImageUrl="screenshots/image.gif" runat="server" />
, the "Error List" pane displays this message:
Attribute 'ImageUrl' is not a valid attribute of element 'PicView'
The property works fine in compiled aspx pages, but how to get rid of this in the VS IDE? And enable IntelliSense for such properties if it's possible?
Got answer here:
ImageUrl needs to be a property, rather than a field, for example:
Public Property ImageUrl As String
Get
Return _imageUrl
End Get
Set(value As String)
_imageUrl = value
End Set
End Property
Private _imageUrl As String
I have a usercontrol for header in the masterpage. I need to an attribute 'linkName' from some of the content pages. ie for some pages it should be
<uc1:AdminHeader ID="Adminheader1" runat="server" linkClass="adminHeaderSelected" link="manageData"></uc1:AdminHeader>
and in few other pages it should be
<uc1:AdminHeader ID="AdminHeader1" runat="server" linkName="adminusers"></uc1:AdminHeader>
how can i acheive this througn content pages?
Add a public property LinkName to your MasterPage that get/set the UserControl's property.
Then you can set it from the page in the following way:
((MyMaster)this.Page.Master).LinkName = "adminusers";
Where MyMaster is the actual type of your MasterPage.
VB.NET:
DirectCast(Me.Page.Master, MyMaster).LinkName = "adminusers"
Edit: If you want to add an non-existing attribute at runtime:
Create a method AddHeaderAttribute:
public void AddHeaderAttribute(string key, string Value)
{
Adminheader1.Attributes.Add(key, Value);
}
VB.NET
Public Sub AddHeaderAttribute(key As String, Value As String)
Adminheader1.Attributes.Add(key, Value)
End Sub
Call this method in the way described above, for example
((MyMaster)this.Page.Master).AddHeaderAttribute("LinkName", "adminusers");
http://msdn.microsoft.com/en-us/library/system.web.ui.usercontrol.attributes.aspx
How can I hide a user control on the master page from a content page? This is code I have on my content page's load.
Dim banner As UserControl = DirectCast(Master.FindControl("uc_banner1"), UserControl)
banner.Visible = True
Does nothing for me :(
Expose the visible property of the user control via a property of the MasterPage.
In your MasterPage:
public bool MyBannerVisibility
{
get { return uc_banner1.Visible; }
set { uc_banner1.Visible = value; }
}
Then make sure to add a reference in your content page:
<%# MasterType TypeName="YourMasterPageTypeName" %>
Then in your content page just do:
Master.MyBannerVisibility = false;
Edit: Since your using VB.net I used a code converter to convert it for you:
Public Property MyBannerVisibility() As String
Get
Return uc_banner1.Visible
End Get
Set
uc_banner1.Visible = value
End Set
End Property
Content Page:
Master.MyBannerVisibility = False
I would like to have an aspx page that contains something like....
<form id="form1" runas=server >
Hello <%= Me.UserName() %>
</form>
and a code-behind something like...
Public Class Somepage
inherits SomeOtherPage
Private Readonly Property UserName() as String
Get
return "Rory"
End Get
End Property
End Class
I have tried this code but the aspx errors claiming that UserName is not declared.
What is the proper way to do this?
Mark the property as Protected, not Private.
Protected Readonly Property UserName() as String
Get
return "Rory"
End Get
End Property