Click here to Skip to main content
11,920,156 members (58,007 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked

Property Generator Tool

, 12 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An approach to minimize issues when using properties in ASP.NET.


This article describes a small tool for generating code for a property whose values are stored in ‘View State’.


During my earlier development experience with creating custom controls for ASP.NET, most of the bugs that I used to find during the later stages of testing (or) in some customer reports were the view state values of properties messing with each other (because of copy/paste code).

This article will illustrate a technique that can be used to avoid this possible issue of storing properties in view state.

Persisting the property value in the ViewState

Say, if you want to create a property named “ExtendedText” and store its value in the view state, you will end up doing the following:

public int ExtendedText
        object extendedText = ViewState (”ExtendedText”];
        return (extendedtext != null) ? (string) extendedtext : "DefaultText”;

        ViewState["ExtendedText"] = value;

Usual issues with the above code

The mistakes we do in writing/copying the above code will result in some minor issues later. Even if there is only a small change in the key of the view state, it cannot be identified during the compilation of the program. For example, an issue cannot be identified in the above program if you write ViewState[“ExtendText”] when setting the View State property (it was “ExtendedText” and not “ExtendText” originally). Each time, we have to write similar code.

The below are the solutions for this problem:

  1. Simplify the code using a common method for getting and setting properties.
  2. public static object GetViewState(StateBag ViewState, 
                  string ViewStateName, object DefaultValue)
        object Value = ViewState[ViewStateName];
        return (Value != null) ? Value : DefaultValue;
    public static void SetViewState(StateBag ViewState, string ViewStateName, 
                                    object DefaultValue,object Value)
        if (Value!=null)
            bool bEquals = (Value.Equals(DefaultValue));
            ViewState[ViewStateName] = (!bEquals) ? Value : null;
  3. Call the methods inside the properties.
  4. public string ExtendedText
            object _Text = GetViewState(this.ViewState, " ExtendedText", string.Empty);
            return (string)_Text;
            SetViewState(this.ViewState, " ExtendedText ", string.Empty, value);

Create the above code using some templates or tools, like the attached sample tool. The tool does the following.

About PropertyGeneratorTool

The tool has the input controls to:

  1. Provide the name of the property
  2. Provide the type
  3. Provide a default value
  4. Select the default value attribute as needed
  5. Select if only the get method should be generated

Once you specify the above details, clicking on the Generate Code button will provide you with the code for the specified property in the RichTextBox control.

How the tool Works

As mentioned before, this is a simple idea and any beginner programmer can do this in their own way. Currently, the code is generated when we click on the Generate Code button in the tool by calling the “GeneratePropertyText” method, which gets the name, type, and the default value text, and creates the code based on the options selected for generating the get and (or) set methods, and generating the default value attribute. You can modify the GenerateGETText/GenerateSETText methods based on the way you want your code to be.

Example: if you have your SetViewState code in another class called BaseManager, then you can modify the following line in the GenerateSETText method:

stb.Append("BaseManager.SetViewState(this.ViewState," + "\"" + 
   this.PropertyName.ToString() + "\"" + "," + 
   _Value1 + "," + "value" + ") ;" );

Sample output






This is just an idea for creating your own templates for the scenario I have mentioned above. There might be some other ways to achieve this, which I might not be aware of. Feel free to send your feedback. Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
I am a software developer from Chennai, India. From Novemeber 2004, I am in to the .NET Framework and I enjoy writing codes in C#. My experience in .NET Technology includes designing and creating custom controls and application for Web and windows platform.

You may also be interested in...

Comments and Discussions

GeneralWould be better as a Code Snippet rather than a separate tool Pin
Alex Furmanski16-Apr-08 5:53
memberAlex Furmanski16-Apr-08 5:53 
Nice idea, but I think it feels a little cumbersome having a separate tool for this. I use a code snippet for my ViewState-backed properties.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="">
	<CodeSnippet Format="1.0.0">
			<Description>Code snippet for property stored in the View State</Description>
			<Author>Alex Furmanski</Author>
					<ToolTip>Property type</ToolTip>
					<ToolTip>Property name</ToolTip>
					<ToolTip>A description of the property</ToolTip>
					<Default>Summary description.</Default>
			<Code Language="csharp"><![CDATA[#region $property$
	/// <summary>
  /// $description$
  /// </summary>
	public $type$ $property$
		get { return ($type$)this.ViewState["$property$"];}
		set { this.ViewState["$property$"] = value;}

Admittedly it lacks the options for default values but is so much nicer to use in Visual Studio. I type vprop-tab-tab and BANG there's my property.
GeneralVB Version Pin
Dhillon14-Apr-08 21:30
memberDhillon14-Apr-08 21:30 
GeneralRe: VB Version Pin
Sivastyle15-Apr-08 4:26
memberSivastyle15-Apr-08 4:26 
GeneralRe: VB Version Pin
Sivastyle16-Apr-08 4:05
memberSivastyle16-Apr-08 4:05 
GeneralAnother way is... Pin
tkrafael_net14-Apr-08 5:04
membertkrafael_net14-Apr-08 5:04 
GeneralRe: Another way is... Pin
Sivastyle15-Apr-08 4:27
memberSivastyle15-Apr-08 4:27 
GeneralRe: Another way is... Pin
Alexandru Matei4-May-09 0:00
memberAlexandru Matei4-May-09 0:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 13 Apr 2008
Article Copyright 2008 by Sivastyle
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid