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?
Can you post the code that you're using to bind the drop down list?
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>
please post the code-behind page ...
ASKER
There is no code behind at this point. That is exactly how the data is bound to the control.
ASKER
The only code-behind that exists at this point in the project is the empty method:
protected void ddlSortBy_SelectedIndexCha nged(objec t sender, EventArgs e)
{
}
protected void ddlSortBy_SelectedIndexCha
{
}
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
Select 'Text' = 'All', Valaue = '-1'
Union All
Select 'Text' = JobTypeName, 'Value' = JobID From JobTypeTable
ASKER
No there is no query. This is a linqdatasource the connects directly to the tables.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.