Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / July 2006

Tip: Looking for answers? Try searching our database.

Dynamically setting selected item in control derived from DropDownList

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mario Vargas - 24 Jul 2006 20:05 GMT
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 - 24 Jul 2006 23:04 GMT
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.

> Hello,
>
[quoted text clipped - 113 lines]
>  }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.