Support Portal ContactGet in touch

Exposing ASP.Net Webcontrol Attributes via Markup

   Words by CRM Consultancy

   on 24/08/2016 18:00:00

The following brief article describes how we make an attribute available in ASPX Markup, as opposed to be set in the Server Code-behind:

 

image

 

So if we have an existing Webcontrol that is available within the Markup:

 

[ParseChildren(true, "")]

[PersistChildren(true)]

[ToolboxData("<{0}:DocumentList runat=server></{0}:DocumentList>")]

public class DocumentsList : WebControl

 

NOTE: The ParseChildren and PersistChildren are important here, as this informs .NET to process the Markup instead our control – as well as the Markup that starts our Control.

 

So allowing:

<asp:DropDownList ID="documentRecordSelector" AutoPostBack="true" runat="server" Height="24px" Width="240px" OnSelectedIndexChanged="documentRecordSelector_onChange">

    <asp:ListItem Text="For Case" Value="case"></asp:ListItem>             

</asp:DropDownList>                   

 

Over a single line statement in mark-up such as:

<asp:DropDownList ID="documentRecordSelector" AutoPostBack="true" runat="server" Height="24px" Width="240px" OnSelectedIndexChanged="documentRecordSelector_onChange" />

 

First of all, we need to specify our code to use the relevant supporting library, in this case System.ComponentModel:

 

using System.ComponentModel;

 

We can then view an attribute in the code for this Webcontrol:

 

public ListDefinition ListDefinition

{

       get

       {

             return this._listDefinition;

       }

       set

       {

             this._listDefinition = value;

       }

}

 

This attribute could be a simple attribute – such as a String, Int or Decimal.

 

Or could (as above) be a complex typed object.

 

[Category("Documents List")]

[DefaultValue(null)]

[Description("The definition for how the Document List will be presented")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[PersistenceMode(PersistenceMode.InnerDefaultProperty)]

public ListDefinition ListDefinition

{

       get

       {

             return this._listDefinition;

       }

       set

       {

             this._listDefinition = value;

       }

}

 

If we are using this type of complex object, then we must ensure that the Object Type is permissible to be used as a Markup Component – so in our example above for the ListDefintion Class, checking the definition of this class:

 

[DefaultProperty("Columns")]

[ParseChildren(true, "Columns")]

[PersistChildren(true)]

[ToolboxData("<{0}:ListDefinition runat=\"server\"> </{0}:ListDefinition>")]

public class ListDefinition : IListDefinition

 

This should then allow us to refer to the Attribute from within the Markup, as opposed to being forced to populate via the Code-behind:

 

ASPX Markup

<crmcs:DocumentsList ID="documentsList" runat="server" TableCssClass="simpleTable" IsClient="true" IsSupplier="false" ShowDocumentDescriptionOnly="true" Width="100%">

    <ListDefinition DefinitionType="FixdColumns" ShowImage="true" FilterMode="true" />

</crmcs:DocumentsList>

 

Allowing this way of defining the List Definition as an object-in-an-object in the Markup, rather than the traditional code-behind approach:

 

C# Code-behind

documentsList.ListDefinition.PrefilterColumns = "'" + SpecificDocumentCategory + "','',''";

documentsList.ListDefinition.FilterMode = true;

documentsList.ListDefinition.sScrollY = 480;

 

This can be useful when working on ASP.Net Webform Projects – as provides a good separation of Code-behind Business Logic vs Presentation Layer Markup; and so helps keep a tighter tiered design of our Web Application.

 

Which, whilst not strict separation of concerns between Business Logic and Presentation (as attributes defined in Mark-up could help drive Business Logic) it does layer our solution better and so make for more transparent easy to maintain code.

 

Further Reading

DesignerSerializationVisibilityAttribute Class (MSDN)

https://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute(v=vs.110).aspx

ASP.Net User Controls Overview (MSDN)

https://msdn.microsoft.com/en-us/library/fb3w5b53.aspx

  • "Paul has made a real difference to how my team of 24 people record and store valuable customer data and sales opportunities. Highly recommended."

    James, Operations Director

  • "Understanding your business allows us to advise when to implement aspects of CRM and, likewise, when not to."

    Paul McQuillan, Managing Director

  • "Dynamics 365 and CRMCS have made a real lasting difference to our business, allowing us to replace older systems that were holding back our performance."

    Grahame, Chief Operating Officer

  • "James worked well with us to help connect CRM with Outlook and relate how this might benefit our team using CRM for Property Care."

    Natalie, Property Care Supervisor

  • "Matt was really good with helping us run User Testing on the new Compliance Module of our CRM System."

    Tom, Compliance Administrator

Prefer to go old-school?

Write to us using the below addresses.

Head Office
CRM Consultancy
61 Oxford Street
Manchester
M1 6EQ

London Office
CRM Consultancy London
Grosvenor Avenue
London

Content © CRM Consultancy.