Link to home
Start Free TrialLog in
Avatar of johnywhite
johnywhite

asked on

Add all to dropdownlist with datasource

I have a dropdownlist that is bound linqdatasource. The dropdownlist is used to only contacts based on a job type that is selected in the dropdown. It works perfectly, however, there are instances where the user wants an all option to display all contacts no matter what their job type. How do I add this and still use the datasource?
Avatar of anoyes
anoyes
Flag of United States of America image

Can you post the code that you're using to bind the drop down list?
Avatar of johnywhite
johnywhite

ASKER


                    <asp:DropDownList ID="ddlSortBy" runat="server" 
                        onselectedindexchanged="ddlSortBy_SelectedIndexChanged" AutoPostBack="True" 
                        DataSourceID="datasourceCrafts" DataTextField="Craft" DataValueField="ID">
                    </asp:DropDownList>
                    <asp:LinqDataSource ID="datasourceCrafts" runat="server" 
                        ContextTypeName="MDataClassesDataContext" OrderBy="Craft" 
                        TableName="crafts">
                    </asp:LinqDataSource>

Open in new window

please post the code-behind page ...
There is no code behind at this point. That is exactly how the data is bound to the control.
The only code-behind that exists at this point in the project is the empty method:
  protected void ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
If there is a Query selecting the JobType from the JobType table then modify it as follows

Select 'Text' = 'All', Valaue = '-1'
Union All
Select 'Text' = JobTypeName, 'Value' = JobID From JobTypeTable

No there is no query. This is a linqdatasource the connects directly to the tables.
I found the solution myself. If anyone finds this in the future here it is:
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If Not IsPostBack Then

            Dim dc As New QSSDataContext
            Dim Emps = (From s In dc.Shifts Where s.Employee.inActive Is Nothing OrElse s.Employee.inActive = False Select New With {s.Employee.name, s.sid} Distinct)

            ddlSortBy.DataSource = Emps.OrderBy(Function(em) em.name)
            ddlSortBy.DataTextField = "name"
            ddlSortBy.DataValueField = "sid"
            ddlSortBy.DataBind()
            ddlSortBy.Items.Insert(0, "All Employees")

        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial