C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Properties In User Controls - A Tip And Trick


Posted Date: 20 Sep 2007    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Muhammad Zeeshanuddin KhanMember Level: Bronze    
Rating: 1 out of 5Points: 5



Introduction


I am writing this article to share with you a special tip and trick to persist the property values in user controls during postbacks.

Wrong Approach



private String myName;

public String MyName
{
get{return myName;}
set{myName = value;}
}


This wrong approach will lose your property values. In addition, whenever you want to get the value of MyName Property it will never get in postbacks and you will always get the Null Exception.

Right Approach



public String MyName
{
get
{
Object o = ViewState["MyName"];
if(o == null)
{
return null;
}
else
{
return (String)o;
}
}
set
{
ViewState["MyName"] = value;
}
}


This right approach persists your property values during postbacks.

Summary


This concludes that whenever you want to use properties in your controls, it is the best approach to use ViewState with your properties. Which will always persist you Property Values.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
User Controls  .  Properties  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Connection Pooling in ASP.NET
Previous Resource: Freezing GridView Column Headers
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use