Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 870871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:32:59+00:00 2026-05-15T10:32:59+00:00

I’m trying to reduce the code duplication that exists throughout my asp.net web forms.

  • 0

I’m trying to reduce the code duplication that exists throughout my asp.net web forms. This is what an example object loaded from the database looks like.

Apartment
  int id
  decimal rent
  bool callForPricing
  int squareFeet
  int beds
  int baths

Now I create views from this object in several different asp.net pages (ie. a list with several apartments, a detail view, etc). In the past what I have done is create another class that wraps the Apartment class. Something like this…

ApartmentView

  Apartment apt

  public virtual String Rent
  {
     get 
     { 
        if (apt.CallForPricing)
        {
           return "Call For Pricing"; 
        }else{
           return apt.Rent.ToString("C") + "/Month";
        }
     }
  }

  public virtual String BedsBathsSqFt
  {
     get 
     { 
        if (apt.squareFeet > 0)
        {
           return apt.beds + " Beds|" + apt.beds + " Beds|" + apt.SquareFeet + " sqft"; 
        }else{
           return apt.beds + " Beds|" + apt.beds + " Beds";
        }
     }
  }

As you can see I’m usually just creating string representations of the data. I have considered having the ApartmentView class extend the Apartment class, but haven’t because of overlapping properties like ‘Rent’. I’m just wondering how people normally deal with this situation. Is this the correct naming convention to use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:33:00+00:00Added an answer on May 15, 2026 at 10:33 am

    This is a tough dilemma to work through on any webforms project. You have two choices for the conditional formatting logic – in the page itself (now you have to duplicate it all over the site), or in some code behind like you’re doing. Neither are great options in terms of separation of concerns, it’s a well known drawback of the ASP.Net webforms model.

    Personally I’d turn your view into an .ascx web user control that contains a FormView control and databind it to your apartment wrapper object. Something like :

    <asp:FormView ID="FormView1"
      DataSourceID="ObjectDataSource1"
      RunAt="server">
    
      <ItemTemplate>
        <table>
          <tr>
            <td align="right"><b>Rent:</b></td>       
            <td><%# Eval("Rent") %></td>
          </tr>
          <tr>
            <td align="right"><b>BedsBathsSqFt:</b></td>     
            <td><%# Eval("BedsBathsSqFt") %></td>
          </tr>
        </table>                 
      </ItemTemplate>                 
    </asp:FormView>
    

    Have the .ascx expose the DataSource property so it could be set by the page that’s using it.

    If Databinding was more flexible with conditional expressions, you could do away with the apartment wrapper object and embed the conditions directly in the user control. With the complexity of your properties though, that will probably be a big headache. However, you could look at something like this, where people have attempted to get around those limitations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.