Dynamically setting selected item in control derived from DropDownList

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;
}
}
 
M

Mario Vargas

I already figured out how to solve my problem. I am storing the assignment
in a member field that is then explictly selected after the elements have
been loaded in the OnLoad() method.



Mario Vargas said:
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top