Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Mario Vargas #1
Dynamically setting selected item in control derived from DropDownList
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;
}
}
Mario Vargas Guest
-
How To Dynamically Set Selected Item in SELECT List inDW Object
<-- Cross=posted on DW Application Development forum-- > Hi! I hope I can explain this situation halfway decently... I am creating a DW... -
Question about a user control derived from DropDownList
I posted this in a different group yesterday with no responses, so I'll try here. I have a simple control that is in a CS file (not ASCX). It is... -
can't set the selected item of the dropdownlist
I generate template columns in datagrid dynamic and I want dropdownlist in the EditItemTemplate, I did it But I can't set the selected item of... -
problem with dropdownlist selected item
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using... -
Setting 'selected' item in bound drop down list
You must set the selected index - meaning, you must know the location of the item that needs to be selected in the list. "ctb"... -
Mario Vargas #2
Re: Dynamically setting selected item in control derived from DropDownList
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" <mariovargas@thecourier.com> wrote in message
news:ufg4jQ1rGHA.148@TK2MSFTNGP04.phx.gbl...> 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;
> }
> }
>
Mario Vargas Guest



Reply With Quote

