Peter Jennings
Peter Jennings

Reputation: 359

How to set initial value as Select in the dropdown of asp.net page that is databound with sql statement

I am binding a dropdown for location with a select statment

Select location_id, location_name,businessid from inventory.tbl_location order by location_name

I want to put the first element as 'Select location'. Right now I am getting all locations. How to set initial value as Select in the dropdown of asp.net page that is databound with sql statement?

In the aspx page :

        <asp:DropDownList ID="ddlAllLocations" runat="server" DataSourceID="SqlDataSourceBusinessLocations"
                            DataTextField="Location_Name" DataValueField="Location_ID" AutoPostBack="True">
                        </asp:DropDownList>

And

 <asp:SqlDataSource ID="SqlDataSourceBusinessLocations" runat="server" ConnectionString="<xxxxxx>"
            ProviderName="<%$ zzzzz %>" SelectCommand="Select location_id, location_name,businessid from inventory.tbl_location order by location_name" FilterExpression="businessid in ({0})">
            <FilterParameters>
                <asp:SessionParameter DefaultValue="0" Name="BUID" SessionField="BusinessUnitIDs" />
            </FilterParameters>
        </asp:SqlDataSource>

I added the code as suggested in the page_load event, here is another problem, everytime it is adding select location to the list items

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack() Then
            lblError.Text = ""
            ddlAllLocations.Items.Insert(0, New ListItem("Select location"))
        End If


    End Sub

Upvotes: 1

Views: 4692

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416149

Try this:

<asp:DropDownList ID="ddlAllLocations" runat="server"
       DataSourceID="SqlDataSourceBusinessLocations"
       DataTextField="Location_Name" 
       DataValueField="Location_ID" 
       AutoPostBack="True"
       AppendDataBoundItems="True">
   <asp:ListItem value="" selected="True">
      Select
   </asp:ListItem>
</asp:DropDownList>

Don't forget the AppendDataBoundItems attribute. Be careful using this in an update panel: each update will re-append all the items and you'll end up with duplicates. In that case, you might be able to fix it by disabling ViewState for the control.

Upvotes: 2

walther
walther

Reputation: 13598

Not sure how you do the databinding but I hope you're doing it in code-behind...

In that case it's pretty straighforward:

mydroplist.DataSource = someSource;
mydroplist.DataBind();
mydroplist.Items.Insert(0, new ListItem("Select location"));

edit based on your edit:
it's not a good idea to have SQL in your UI.. That's a very bad design. Do some research on proper programming architectures, how to separate layers etc. Then you will do the databinding in code-behind and my sample will help you.

If you insist on using your way of doing things, you can simply wire up the event when the dropdownlist is databound and add this piece of code (except the binding part of course)

Upvotes: 1

Related Questions