Introduction
This article describes a small tool for generating code for a property whose values are stored in ‘View State’.
Background
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
{
get
{
object extendedText = ViewState ("ExtendedText"];
return (extendedtext != null) ? (string) extendedtext : "DefaultText";
}
set
{
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:
- Simplify the code using a common method for getting and setting properties.
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;
}
}
- Call the methods inside the properties.
public string ExtendedText
{
get
{
object _Text = GetViewState(this.ViewState, " ExtendedText", string.Empty);
return (string)_Text;
}
set
{
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:
- Provide the name of the property
- Provide the type
- Provide a default value
- Select the default value attribute as needed
- 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
Conclusion
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.
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.