Dynamically setting selected item in control derived from DropDownList

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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"...
  3. #2

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139