I have a composite control that has a large number of properties that can be used to style the control. I want to group these properties yet still maintain some of the properties in the ViewState
The markup for the control would look like this:
e.g.
<cc:Test id="test">
<Toolbar Items="add,delete" Enabled="true" />
<Grid Enabled="true" AllowSort="true" AllowFilter="true" />
</cc:Test>
My code looks something like this
<ParseChildren(true)> <PersistChildren(true)> _
Public Class Test Inherits CompositeControl
Private _grid As New GridStyle();
<PersistenceMode(PersistenceMode.InnerProperty)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Grid As GridStyle
Get
Return _grid;
End Get
End Property
End Class
Public Class GridStyle
private _allowFilter As Boolean = False;
Public Property AllowFilter As Boolean
Get
Return _allowFilter
End Get
Set(value As Boolean)
_allowFilter = value
End Set
End Property
End Class
ViewState is not accessible from the GridStyle class so how would I maintain the state of the AllowFilter property in the ViewState?
In your custom control (or make wrappers for standard controls used within a custom control) you need to override SaveViewState and LoadViewState
This is well documented on MSDN and the web in general
Related
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"
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
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
I created several user controls - most containing a single web control (text box, drop down, radio button etc) - along with one or more validation controls. The point being to combine control and validation in a single user control.
I created a base class for these user control with some common functionality - setters for several properties of a single web control, specifically CssClass and Style to be set in the control in the ascx.
Eg a single text box with a single required field validator.
Sample code for the base class:
public WebControl ctrl {get; set;} //allow derived class access to this
public string CssClass
{
set { ctrl.CssClass = value; } //allow CssClass to be set in the aspx page
}
Sample code for derived class:
(in constructor or control OnInit Event - or ?)
base.ctrl = txt; //tell the base class which web control to apply common properties to.
public string ErrorMessage
{
set { val.ErrorMessage = value;} //this works !
}
Sample code for ascx:
<asp:TextBox ID="txt" Cssclass="input-text-m" maxlength="50" runat="server" />
<asp:RequiredFieldValidator ID="val" runat="server" ControlToValidate="txt"
ErrorMessage="">*</asp:RequiredFieldValidator>
Sample code for aspx:
<uc:TextBox ID="ForeName" Cssclass="input-text-m" maxlength="50"
ErrorMessage="Forename" runat="server"/>
The problem I found was that I couldn't find a way for the derived class to set the base class web control reference before the base classes property setters are called.
If I set base.ctrl in the derived class constructor - then the derived class control reference (txt) is still null at this point.
If I set base.ctrl in any of the control events - eg OnInit - then this is too late.
So far I have got around the problem by simply not using a base class, and writing the property setter code in the user control class instead, however this means duplication of code, which I was trying to avoid.
Is there a way to inform the base class of the control I want it to set the properties for in advance of them being set - or am I going about things the wrong way...
What about calling EnsureChildControls before any get/set operations and including the set operation for ctrl = txt in EnsureChildControls? This is pretty standard practice for a normal servercontrol, I would think it would work for UserControls too.
public string CssClass { set { EnsureChildControls(); ctrl.CssClass = value; } }
Override EnsureChildControls, leaving in the call to base, and set ctrl = txt; here after the call to base.
More information: http://msdn.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx
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