How to make first combo box entry blank?

Discussion in 'ASP .Net' started by Randall Parker, Oct 31, 2005.

  1. Using DropDownList to create select/option tags in HTML how to make the first option
    be a blank string?

    I have fields with optional values. Currently I'm populating the DropDownList by
    querying out a list of choices from a table. But the table shouldn't have a blank row.

    A couple of possibilities:

    1) Use a UNION query. I've done this in MS Access with combo boxes and the first
    member of the union returns a single row. I can't remember the SQL syntax for
    returning a single row that is not from a table though. Anyone know how?

    2) Populate the DropDownList in CodeBehind without a bind of a result set. Just add a
    blank row and then do a foreach on the dataset that has the real rows and add all them.

    3) Some other approach?
     
    Randall Parker, Oct 31, 2005
    #1
    1. Advertising

  2. Randall Parker

    Jason Kester Guest

    #1 requires your datalayer to know about presentation, so that's out.

    #2 works fine, and will score you points with oldskool purists and php
    kids, but it's extra work compared to...

    #3 get your dataset from the database and cram an extra row into it
    before binding:


    private void PopulateFamilyList()
    {
    DataSet ds = Family.List();
    ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
    DataView dv = ds.Tables[0].DefaultView;
    dv.Sort = Family.Columns.FamilyName;

    selFamilyID.DataSource = dv;
    selFamilyID.DataValueField = Family.Columns.FamilyID;
    selFamilyID.DataTextField = Family.Columns.FamilyName;
    selFamilyID.DataBind();
    }


    Good luck!

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/

    ---
    Get your own Travel Blog, with itinerary maps and photos!
    http://www.blogabond.com/
     
    Jason Kester, Oct 31, 2005
    #2
    1. Advertising

  3. Randall Parker

    Siva M Guest

    After data binding, DropDownList.Items.Inser (0, "")

    "Randall Parker" <NOtechieSPAMpundit_please@future_avoidjunk_pundit.com>
    wrote in message news:%...
    Using DropDownList to create select/option tags in HTML how to make the
    first option
    be a blank string?

    I have fields with optional values. Currently I'm populating the
    DropDownList by
    querying out a list of choices from a table. But the table shouldn't have a
    blank row.

    A couple of possibilities:

    1) Use a UNION query. I've done this in MS Access with combo boxes and the
    first
    member of the union returns a single row. I can't remember the SQL syntax
    for
    returning a single row that is not from a table though. Anyone know how?

    2) Populate the DropDownList in CodeBehind without a bind of a result set.
    Just add a
    blank row and then do a foreach on the dataset that has the real rows and
    add all them.

    3) Some other approach?
     
    Siva M, Oct 31, 2005
    #3
  4. Randall Parker

    Jason Kester Guest

    Siva M wrote:
    > After data binding, DropDownList.Items.Inser (0, "")


    I'd recommend against this. DataBound controls flip out sometimes when
    you modify them after binding.

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/
     
    Jason Kester, Oct 31, 2005
    #4
  5. Jason,

    I like the idea of inserting a row in the dataset. But why are you doing a Sort? If
    I've already queried out by dataset in my preferred sort order is it unnecessary and
    perhaps even unwise to do the sort?

    Also, when you do this:

    ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);

    I do not see how the column in the new row ever gets set to an empty string. Is that
    just by default?

    Mind you, I'm an ADO.Net novice along with being an ASP.Net novice (I'm coming from
    C++ and Java and know ODBC, JDBC, DAO, ADO and a few other database api sets fwiw).
    So maybe I'm missing something obvious.

    Jason Kester wrote:
    > #1 requires your datalayer to know about presentation, so that's out.
    >
    > #2 works fine, and will score you points with oldskool purists and php
    > kids, but it's extra work compared to...
    >
    > #3 get your dataset from the database and cram an extra row into it
    > before binding:
    >
    >
    > private void PopulateFamilyList()
    > {
    > DataSet ds = Family.List();
    > ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
    > DataView dv = ds.Tables[0].DefaultView;
    > dv.Sort = Family.Columns.FamilyName;
    >
    > selFamilyID.DataSource = dv;
    > selFamilyID.DataValueField = Family.Columns.FamilyID;
    > selFamilyID.DataTextField = Family.Columns.FamilyName;
    > selFamilyID.DataBind();
    > }
    >
    >
    > Good luck!
    >
    > Jason Kester
    > Expat Software Consulting Services
    > http://www.expatsoftware.com/
    >
    > ---
    > Get your own Travel Blog, with itinerary maps and photos!
    > http://www.blogabond.com/
    >
     
    Randall Parker, Oct 31, 2005
    #5
  6. > I'd recommend against this. DataBound controls flip out sometimes when
    > you modify them after binding.


    Jason , just curious on this point .. I got a page with 17 dropdown lists
    (users choice!) databound and then a "Please select..." option added with a
    code similar to below.. did not face any problem so far.. have you come
    across any specific issues?


    "Jason Kester" wrote:

    > Siva M wrote:
    > > After data binding, DropDownList.Items.Inser (0, "")

    >
    > I'd recommend against this. DataBound controls flip out sometimes when
    > you modify them after binding.
    >
    > Jason Kester
    > Expat Software Consulting Services
    > http://www.expatsoftware.com/
    >
    >
     
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 31, 2005
    #6
  7. Jason,

    Here's what seems to work for me. Does this seem right to you?

    dbCmd.CommandText = "SELECT brand_name FROM mydb.BrandName ORDER BY
    brand_name";
    DataSet dsBrandNameDataSet = new DataSet();
    dbAdapter.SelectCommand = dbCmd;
    dbAdapter.Fill(dsBrandNameDataSet);

    // Insert a blank row here.
    DataRow TopRow = dsBrandNameDataSet.Tables[0].NewRow();
    TopRow["brand_name"] = "";
    dsBrandNameDataSet.Tables[0].Rows.InsertAt(TopRow,0);

    BrandNameList.DataSource = dsBrandNameDataSet;
    BrandNameList.DataTextField = "brand_name";
    BrandNameList.DataBind();


    Jason Kester wrote:
    > #1 requires your datalayer to know about presentation, so that's out.
    >
    > #2 works fine, and will score you points with oldskool purists and php
    > kids, but it's extra work compared to...
    >
    > #3 get your dataset from the database and cram an extra row into it
    > before binding:
    >
    >
    > private void PopulateFamilyList()
    > {
    > DataSet ds = Family.List();
    > ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
    > DataView dv = ds.Tables[0].DefaultView;
    > dv.Sort = Family.Columns.FamilyName;
    >
    > selFamilyID.DataSource = dv;
    > selFamilyID.DataValueField = Family.Columns.FamilyID;
    > selFamilyID.DataTextField = Family.Columns.FamilyName;
    > selFamilyID.DataBind();
    > }
    >
    >
    > Good luck!
    >
    > Jason Kester
    > Expat Software Consulting Services
    > http://www.expatsoftware.com/
    >
    > ---
    > Get your own Travel Blog, with itinerary maps and photos!
    > http://www.blogabond.com/
    >
     
    Randall Parker, Oct 31, 2005
    #7
  8. Randall Parker

    Jason Kester Guest

    "Sometimes" is the operative word here. "Occasionally" might even have
    been a better choice.

    I ran into this a couple years back, and switched to the other method
    with better success. If it's working for you, then I wouldn't sweat
    it. Just something to keep in mind if those boxes start acting up in
    the future!

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/
     
    Jason Kester, Oct 31, 2005
    #8
  9. Randall Parker

    Jason Kester Guest

    Sorry if my code was misleading. I just pasted a fragment from one of
    my pages. I never use ad-hoc SQL (and would recommend you avoid it
    too), so I sometimes find it easier to sort the results of a Stored
    Procedure call locally, provided it's only a few records.

    Looks like you're headed in the right direction. You're correct in
    assuming that .NewRow()s come in blank, so the approach you take would
    be the only way to set the text to "-- please select a value --" or
    something similar.

    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/

    ---
    Get your own Travel Blog, with itinerary maps and photos!
    http://www.blogabond.com/
     
    Jason Kester, Oct 31, 2005
    #9
  10. Randall Parker

    Greg Burns Guest

    Just wanted to say I always do it after databinding. By using the
    DropDownList.Items.Insert(0, "-- please select a value --") trick. Never
    had a problem doing it that way. Also, I would think it is kinda necessary
    if binding using a datareader...

    Greg


    "Jason Kester" <> wrote in message
    news:...
    > "Sometimes" is the operative word here. "Occasionally" might even have
    > been a better choice.
    >
    > I ran into this a couple years back, and switched to the other method
    > with better success. If it's working for you, then I wouldn't sweat
    > it. Just something to keep in mind if those boxes start acting up in
    > the future!
    >
    > Jason Kester
    > Expat Software Consulting Services
    > http://www.expatsoftware.com/
    >
     
    Greg Burns, Nov 1, 2005
    #10
  11. Randall Parker

    Mr Newbie Guest

    There is no reason why there should be a problem 'AFAIK'. Databinding is
    only single direction on ASP.NET so I really dont see why this slould be an
    issue once the page has been rendered

    By contrast, try doing with with a Windows Forms application and things get
    a little more tricky !

    Regards Mr N . . .


    "Greg Burns" <> wrote in message
    news:%...
    > Just wanted to say I always do it after databinding. By using the
    > DropDownList.Items.Insert(0, "-- please select a value --") trick. Never
    > had a problem doing it that way. Also, I would think it is kinda
    > necessary if binding using a datareader...
    >
    > Greg
    >
    >
    > "Jason Kester" <> wrote in message
    > news:...
    >> "Sometimes" is the operative word here. "Occasionally" might even have
    >> been a better choice.
    >>
    >> I ran into this a couple years back, and switched to the other method
    >> with better success. If it's working for you, then I wouldn't sweat
    >> it. Just something to keep in mind if those boxes start acting up in
    >> the future!
    >>
    >> Jason Kester
    >> Expat Software Consulting Services
    >> http://www.expatsoftware.com/
    >>

    >
    >
     
    Mr Newbie, Nov 1, 2005
    #11
  12. Randall Parker

    Jason Kester Guest

    You know what, I bet you're right. I probably came across that doing
    winform development, and was so traumatized that it just left this Big
    Black Mark of Dread in my consciousness. "Here Be Dragaons" and all
    that, and it's haunted me all the way to web development!

    So yeah, I'll agree that I was probably talking out my arse a few posts
    back. Bind away!

    Jason
     
    Jason Kester, Nov 1, 2005
    #12
  13. I am using Visual Studio 2005 and by using the new datasource wizard I have
    been able to cut back on alot of coding (i.e. I don't have to perform any of
    the databinding stuff in by codebehind). I am now faced with this issue where
    I can't add a balnk row unless I do it programatically. I would think that
    with all of the improvements made to the Visual Studio 2005 IDE and to the
    ..NET Framework 2.0 that Microsoft would not have left out the ability to
    accept hard-coded ListItems along with ones that were retrieved from a data
    store after calling the databind() method. Anyone know how to do this?

    "Randall Parker" wrote:

    > Using DropDownList to create select/option tags in HTML how to make the first option
    > be a blank string?
    >
    > I have fields with optional values. Currently I'm populating the DropDownList by
    > querying out a list of choices from a table. But the table shouldn't have a blank row.
    >
    > A couple of possibilities:
    >
    > 1) Use a UNION query. I've done this in MS Access with combo boxes and the first
    > member of the union returns a single row. I can't remember the SQL syntax for
    > returning a single row that is not from a table though. Anyone know how?
    >
    > 2) Populate the DropDownList in CodeBehind without a bind of a result set. Just add a
    > blank row and then do a foreach on the dataset that has the real rows and add all them.
    >
    > 3) Some other approach?
    >
     
    =?Utf-8?B?QW1pIFNjaHJlaWJlcg==?=, Jan 13, 2006
    #13
  14. Randall Parker

    paintycan

    Joined:
    Sep 10, 2006
    Messages:
    1
    This worked for me to enter a blank first entry followed by the databound data, with no extra code behind

    <asp: DropDownList ID="ddlEvents" runat="server" DataSourceID="EventsDS" DataTextField="event_name" DataValueField="event_id" AppendDataBoundItems="True">
    <asp:ListItem></asp:ListItem>
    </asp: DropDownList>

    The empty ListItem ends up being the blank entry at the top of the DropDownList


     
    paintycan, Sep 10, 2006
    #14
    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. Craig G
    Replies:
    3
    Views:
    3,004
    =?Utf-8?B?TGVlIEg=?=
    Jan 16, 2005
  2. Replies:
    0
    Views:
    291
  3. Replies:
    0
    Views:
    267
  4. dthmtlgod

    Fill Combo Box based on another Combo Box

    dthmtlgod, Nov 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    594
    Bob Barrows [MVP]
    Nov 16, 2005
  5. SM
    Replies:
    2
    Views:
    372
Loading...

Share This Page