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

  • Home
  • SEARCH
  • 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 6559045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:16:27+00:00 2026-05-25T13:16:27+00:00

I’m building some web user controls and just wondering what is the right /

  • 0

I’m building some web user controls and just wondering what is the right / best practice approach to implement properties. In this example he control is a “score card” control which has to display a score ( and it also has to do other stuff) ….to make things easier I made these code samples very simple but in reality my control does other stuff as well with the score besides displaying it in a label 🙂

Choice #1
private int _score;
public int Score
{
 get { return _score;  }
 set { _score = value; Refresh(); }
}
public void Refresh()
{
 lblScore.Text = Score;
}


Choice #2:
public int Score {get;set;}
protected void PageLoad(object sender, EventArgs e)
{
Refresh();
}
private void Refresh()
{
 lblScore.Text = Score;
}


Choice #3:
       public int Score
    {
     get { lblScore.Text; }
     set { lblScore.Text = value; }
    }

So , of course, the question is what is the best practice way of implementing the Score property of the control ….:-)

MadSeb

  • 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-25T13:16:28+00:00Added an answer on May 25, 2026 at 1:16 pm

    Choice 1

    • Choice 2 is out of this because you should not link initialization and functionality to specific page events in a UserControl. That is a source for nasty errors related to Page-Lifecycle. You might want to make modifications after Page_Load(f.e. in a Button’s Click-Event in the page) but it’s too late for the implicit Refresh.
    • Choice 3 is out because for simply setting the Text of a Label you don’t want somebody to remember that he has to call Refresh after he has set the Score. But that depends on how expensive “my control does other stuff as well with the score besides displaying it in a label” is. If you often change the Score but not necessarily need to refresh immediately, i would do the Refresh after all initialization has done.

    In my opinion a UserControl should encapsulate complexity as long as it keeps enough flexibility and control to be reusable. Don’t do too much “magic” things in the background that might cause errors in different conditions that you won’t find quickly. That is particularly so in a Setter what normally only should set the corresponding variable.

    There are two different use cases for an UserControl:

    1. Reusability.
      If your control contains only few controls but you want to reuse it many times, i would let the controller get/set properties and don’t do complex things in it that depend on specific conditions. That would reduce reusability. You might want to provide clear methods and events that the controller could handle.
    2. Container.
      If your control behaves similar to a page and has a lot of controls and functionality, it should do most of all by itself. You only want to provide a few methods and events(that don’t have to be handled necessarily). The most important method in this case would be e.g. a public void BindData() that does all initialization after the controller has set the necessary variables. That is the replacement for your Choice 2.

    Note: if your score is stored in lblScore.Text as string anyway, i would prefer using the Text property of the label instead of creating another int-variable(cast it to an int in the getter). That has the advantages that you don’t need to store your variable in ViewState manually, because it’s already stored. On this way you don’t need to set it on every postback.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build

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.