M
Mario Vargas
Hello,
I wrote a control derived from the ASP.NET DropDownList. I want to be able
to automatically databind the derived dropdown list and then
programmatically set the selected item through a property in this control. I
am not sure what the correct order of initialization should be. If I load
all the items in the OnLoad() method, then the property is executed first
and my list's selected index is 0. If I load everything in the OnInit()
method, then I don't know if it's a good programming practice to load the
items for every request instead of letting the control's viewstate handle
this. What's your recommendation? I am using VS.NET 1.1 and here's the code
I am using...
Thanks...
public class USStateDropDownList : DropDownList
{
USStateWithDataDataSet stateDS;
protected override void OnInit(EventArgs e)
{
base.OnInit( e );
stateDS = GetData();
Page.Response.Write( "1<br />" );
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad( e );
if( !Page.IsPostBack || !Page.EnableViewState )
{
LoadItems();
}
Page.Response.Write( "2<br />" );
}
/// <summary>
/// Loads the drop-down list's items into the control.
/// </summary>
private void LoadItems()
{
DataValueField = stateDS.USState.StateCodeColumn.ColumnName;
DataTextField = stateDS.USState.StateNameAndCountColumn.ColumnName;
DataSource = stateDS.USState;
DataBind();
ListItem defaultItem = new ListItem( "- Choose a State -",
String.Empty );
Items.Insert( 0, defaultItem );
SelectedIndex = 0;
}
/// <summary>
/// Gets the US State data from the database.
/// </summary>
private USStateWithDataDataSet GetData()
{
USStateWithDataDataSet stateDS = null;
string cacheKey = "USStateWithData";
if( null == Page.Cache[ cacheKey ] )
{
SubdivisionDataProvider subdivData = new SubdivisionDataProvider();
// Obtain the data from the database
stateDS = subdivData.GetRegionsWithData();
// Add the state data to the cache.
Page.Cache.Add(
cacheKey,
stateDS,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromHours( 3D ),
CacheItemPriority.Normal, null );
}
else
{
// Retrieve the data from the cache.
stateDS = (USStateWithDataDataSet)Page.Cache[ cacheKey ];
}
return stateDS;
}
/// <summary>
/// Gets or sets the USPS 2-character code of the selected state.
/// </summary>
[Description("The selected state as a 2 character code.")]
public string SelectedStateCode
{
get
{
return SelectedValue;
}
set
{
Page.Response.Write( "3<br />" );
ListItem myItem = Items.FindByValue(value.ToUpper());
SelectedIndex = -1;
if( null != myItem )
{
myItem.Selected = true;
}
}
}
public USStateDropDownList()
{
stateDS = null;
}
}
I wrote a control derived from the ASP.NET DropDownList. I want to be able
to automatically databind the derived dropdown list and then
programmatically set the selected item through a property in this control. I
am not sure what the correct order of initialization should be. If I load
all the items in the OnLoad() method, then the property is executed first
and my list's selected index is 0. If I load everything in the OnInit()
method, then I don't know if it's a good programming practice to load the
items for every request instead of letting the control's viewstate handle
this. What's your recommendation? I am using VS.NET 1.1 and here's the code
I am using...
Thanks...
public class USStateDropDownList : DropDownList
{
USStateWithDataDataSet stateDS;
protected override void OnInit(EventArgs e)
{
base.OnInit( e );
stateDS = GetData();
Page.Response.Write( "1<br />" );
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad( e );
if( !Page.IsPostBack || !Page.EnableViewState )
{
LoadItems();
}
Page.Response.Write( "2<br />" );
}
/// <summary>
/// Loads the drop-down list's items into the control.
/// </summary>
private void LoadItems()
{
DataValueField = stateDS.USState.StateCodeColumn.ColumnName;
DataTextField = stateDS.USState.StateNameAndCountColumn.ColumnName;
DataSource = stateDS.USState;
DataBind();
ListItem defaultItem = new ListItem( "- Choose a State -",
String.Empty );
Items.Insert( 0, defaultItem );
SelectedIndex = 0;
}
/// <summary>
/// Gets the US State data from the database.
/// </summary>
private USStateWithDataDataSet GetData()
{
USStateWithDataDataSet stateDS = null;
string cacheKey = "USStateWithData";
if( null == Page.Cache[ cacheKey ] )
{
SubdivisionDataProvider subdivData = new SubdivisionDataProvider();
// Obtain the data from the database
stateDS = subdivData.GetRegionsWithData();
// Add the state data to the cache.
Page.Cache.Add(
cacheKey,
stateDS,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromHours( 3D ),
CacheItemPriority.Normal, null );
}
else
{
// Retrieve the data from the cache.
stateDS = (USStateWithDataDataSet)Page.Cache[ cacheKey ];
}
return stateDS;
}
/// <summary>
/// Gets or sets the USPS 2-character code of the selected state.
/// </summary>
[Description("The selected state as a 2 character code.")]
public string SelectedStateCode
{
get
{
return SelectedValue;
}
set
{
Page.Response.Write( "3<br />" );
ListItem myItem = Items.FindByValue(value.ToUpper());
SelectedIndex = -1;
if( null != myItem )
{
myItem.Selected = true;
}
}
}
public USStateDropDownList()
{
stateDS = null;
}
}