Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an object using markup in ASP.NET

Assume we have a Person class:

public class Person
{
  public string FamilyName {get;set;}
  public string GivenName {get;set;}
}

And there's a control to somehow display the information of a list of persons. Here is the pseudocode of the aspx:

<uc1:EmployeesViewer runat="server">
  <Employees>
    <Person>
      <GivenName>John</GivenName>
      <FamilyName>Kerry</GivenName>
    </Person>
    <Person>
      <GivenName>Jack</GivenName>  
      <FamilyName>Lew</GivenName>
    </Person>
  <Employees>
</uc1:EmployeesViewer>

EmployeesViewer.Employees is of type List<Person>, with the attribute [PersistenceMode(PersistenceMode.InnerProperty)].

But Visual Studio doesn't compile this. Is it possible to declare a Person object using markup?

like image 298
Gqqnbig Avatar asked Apr 06 '16 21:04

Gqqnbig


People also ask

What is markup in asp net?

Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.

What is ASP NET syntax?

The Razor Syntax, Server Code, and ASP.NET Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.


1 Answers

Is it possible to declare a Person object using markup?

Yes, with only some minor changes to the markup (and no changes to your Person class):

<%@ Page Language="C#" CodeBehind="DemoPage.aspx.cs" Inherits="DemoApp.DemoPage" %>
<%@ Register TagPrefix="uc1" Assembly="DemoApp" Namespace="DemoApp" %>

<uc1:EmployeesViewer runat="server">
    <Employees>
        <uc1:Person GivenName="John" FamilyName="Kerry" />
        <uc1:Person GivenName="Jack" FamilyName="Lew" />
    </Employees>
</uc1:EmployeesViewer>

The uc1: tag prefix assumes that your Person class is in the same assembly and namespace as EmployeesViewer. If not, just <%@ Register %> another tag prefix for Person.

Note: You can use the <uc1:Person> syntax even though Person doesn't derive from Control:

namespace DemoApp
{
    public class Person
    {
        public string FamilyName {get;set;}
        public string GivenName {get;set;}
    }
}

Here's a simple EmployeesViewer control that renders the list of employees:

namespace DemoApp
{
    [ParseChildren(true)]
    [PersistChildren(false)]
    public class EmployeesViewer : Control
    {
        private readonly List<Person> _employees = new List<Person>();

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public List<Person> Employees
        {
            get { return _employees; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            foreach (Person person in this.Employees)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.P);
                writer.WriteEncodedText(string.Format("{0} {1}", person.GivenName, person.FamilyName));
                writer.RenderEndTag();
            }
        }
    }
}

The [ParseChildren(true)] attribute is needed so that at run time ASP.NET interprets the <Employees> tag as a property name rather than as literal markup to be rendered.

The [PersistChildren(false)] and [PersistenceMode(PersistenceMode.InnerProperty)] attributes are needed so that, if at design time you modify the Employees collection using the designer's Properties window, Visual Studio correctly persists the collection to markup.

like image 119
Michael Liu Avatar answered Oct 11 '22 13:10

Michael Liu