dropdownlist default selected

Discussion in 'ASP .Net' started by =?Utf-8?B?aHV6eg==?=, Nov 1, 2004.

  1. How do i make a dropdownlist selected value based on the value i retrive from
    the database.

    Basically i have an edit page and like to display the default value in a
    dropdown list from the database.

    for example: if the ddl_value is 2 from the database i want the second list
    item to be selected by default.


    <asp:DropDownList id="ddl" runat="server">
    <asp:ListItem Value="0">value 1</asp:ListItem>
    <asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
    selected
    <asp:ListItem Value="0">value 3</asp:ListItem>
    <asp:ListItem Value="1">value 4</asp:ListItem>
    </asp:DropDownList>

    I am using the dropdownlist inside a repeater.. and only retriving single
    record at a time based on a querystring... and using a datareader to fill the
    form.

    Many thanks in advance..
    =?Utf-8?B?aHV6eg==?=, Nov 1, 2004
    #1
    1. Advertising

  2. Hi,

    You need to find the item that has the text value "value 2" and then get its
    index value. Once you have the index value, you can set the SelectedIndex to
    that value.

    Here's a quick way to accomplish all three:

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    If Not IsPostBack Then
    ddl.SelectedIndex = ddl.Items.IndexOf _
    (ddl.Items.FindByText("value 2"))
    End If
    End Sub

    <asp:DropDownList id="ddl" runat="server">
    <asp:ListItem Value="0">value 1</asp:ListItem>
    <asp:ListItem Value="1">value 2</asp:ListItem>
    <asp:ListItem Value="0">value 3</asp:ListItem>
    <asp:ListItem Value="1">value 4</asp:ListItem>
    </asp:DropDownList>

    Does this help?

    Ken
    Microsoft MVP [ASP.NET]



    "huzz" <> wrote in message
    news:...
    > How do i make a dropdownlist selected value based on the value i retrive
    > from
    > the database.
    >
    > Basically i have an edit page and like to display the default value in a
    > dropdown list from the database.
    >
    > for example: if the ddl_value is 2 from the database i want the second
    > list
    > item to be selected by default.
    >
    >
    > <asp:DropDownList id="ddl" runat="server">
    > <asp:ListItem Value="0">value 1</asp:ListItem>
    > <asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
    > selected
    > <asp:ListItem Value="0">value 3</asp:ListItem>
    > <asp:ListItem Value="1">value 4</asp:ListItem>
    > </asp:DropDownList>
    >
    > I am using the dropdownlist inside a repeater.. and only retriving single
    > record at a time based on a querystring... and using a datareader to fill
    > the
    > form.
    >
    > Many thanks in advance..
    >
    >
    Ken Cox [Microsoft MVP], Nov 1, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?aHV6eg==?=

    Chris Austin Guest

    You have to use either FindByValue() or FindByText() method of the items
    collection. If found, it returns the ListItem otherwise, the method returns
    null.

    ListItem item = MyDropDown.items.FindByValue("TX");
    if(item != null)
    {
    MyDropDown.SelectedItem.Selected = false;
    item.Selected = true;
    }

    Here is the reference link to the FindByValue and FindByText methods
    http://msdn.microsoft.com/library/d...lslistitemcollectionclassfindbyvaluetopic.asp

    HTH
    -Chris
    ~
    http://weblogs.austinspad.com/caustin

    "huzz" <> wrote in message
    news:...
    > How do i make a dropdownlist selected value based on the value i retrive

    from
    > the database.
    >
    > Basically i have an edit page and like to display the default value in a
    > dropdown list from the database.
    >
    > for example: if the ddl_value is 2 from the database i want the second

    list
    > item to be selected by default.
    >
    >
    > <asp:DropDownList id="ddl" runat="server">
    > <asp:ListItem Value="0">value 1</asp:ListItem>
    > <asp:ListItem Value="1">value 2</asp:ListItem> // make this dynamically
    > selected
    > <asp:ListItem Value="0">value 3</asp:ListItem>
    > <asp:ListItem Value="1">value 4</asp:ListItem>
    > </asp:DropDownList>
    >
    > I am using the dropdownlist inside a repeater.. and only retriving single
    > record at a time based on a querystring... and using a datareader to fill

    the
    > form.
    >
    > Many thanks in advance..
    >
    >
    Chris Austin, Nov 1, 2004
    #3
  4. =?Utf-8?B?aHV6eg==?=

    ramzihawa

    Joined:
    Feb 18, 2009
    Messages:
    1
    solution to this issue

    hello all,
    The solution is to add OnDataBound="SomeFunction" in your aspx (or ascx) file.

    a sample dropdownlist would be

    <asp: DropDownList ID="ddlPages" runat="server" DataSourceID="SqlDsListPagesOfThisModule"
    DataTextField="ModulePageTitle" DataValueField="ModulePageId"
    AutoPostBack="True" OnDataBound="RamOne" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged" >
    </asp: DropDownList>
    //note that asp :dropdownlist DOES NOT contain spaces but the post replaces it with a smiley
    then in the code behind

    protected void RamOne(System.Object sender, System.EventArgs e)
    {

    if (!Page.IsPostBack)
    {

    if (ddlPages.Items.Count > 0)
    {
    ddlPages.SelectedIndex = 0;//the default value is -1 for nothing selected
    //selectedindexchanged will NOT be triggered automatically
    //so we trigger it (which is exactly what we need !!!!)
    ddlPages_selectedindexchanged(sender,e);


    }

    }


    }


    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
    //force showing the list of dbinteractions on first load (!postback, before page load because this is called from ondatabaound)
    if (!Page.IsPostBack)
    {
    something.databind();//for example Gridview1 or dropdownlist2 etc
    }
    else
    {
    //put here what you would normally put, for example something.databind()
    //and for example a call to a function that logs preferences in a db for this user ("update memberprefs set dropdownvalue= "+ddlPages.selectedvalue+"...where userid=...");
    }
    }


    good luck coding!!!
    Eng. Ramzi Hawa
    Database SqL 2005 expert, Dnn expert...
    email ramzi at charlottecoders.com
    website rolamzi.com
    Last edited: Feb 18, 2009
    ramzihawa, Feb 18, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVu?=
    Replies:
    1
    Views:
    6,012
    Elliot Rodriguez
    May 19, 2006
  2. Iain
    Replies:
    3
    Views:
    929
  3. Replies:
    4
    Views:
    595
  4. Nathan Sokalski
    Replies:
    0
    Views:
    608
    Nathan Sokalski
    Oct 5, 2008
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    290
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page