Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

properties on user control

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
I have 3 properties on my user control that the aspx page sets. Once the user control page gets called whats the best way to save these values? Should it be saved to hidden fields, Sessions?

Thanks.
 
What do you mean save the values? The property is set on the user control and is accessable anytime by calling the property.
 
If this is a public property with set/get attributes you can access it from your UserControl. Why do you need to save them separately? What exactly you're trying to achieve?
 
I thought that after a postback the value that I set in a property would be lost. Thats why user's do this:

public string ProductName
{
get
{
if (ViewState["ProductName"] != null)
{
return ViewState["ProductName"];
}
else
{
return null;
}
}
set
{
ViewState["ProductName"] = value;
}
}
 
I don't have this problem for my UserControls. However, all my textboxes/dropdownlists have EnableViewState property set to true. I guess if you set this property to false you may lose values.
 
It has to do with viewstate on the User Control itself, not the indivdual objects. Make sure you have EnableViewState = True for the user control.
 
and if the user control is added to the page at runtime (vs. design time) if it's added at runtime make sure you add it in the Init event and assign the control an ID. Then viewstate will work properly with the control. (also if adding the control at runtime it must be added with each postback).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
jbenson - the user control viewstate isn't by default = true? would I set the viewstate = true on the aspx page where I am using the user control?

Thanks.
 
viewstate is enabled by default. this can be overriden in a number of places though.
web.config
page declaration
page codebehind
user control declaration
user control codebehind

post the relative code for the page and user control and we may be able to diagnose this better.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
the usercontrol's viewstate is set to true.

could this be because I don't have IIS installed, but am using the local asp.net ports?

thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top