Solved

Want to add a static item to a dynamic drop down list?

Posted on 2011-02-24
7
842 Views
Last Modified: 2012-05-11
I have a drop down list that is dynamically filled from a datasource (SqlDS2) and want to add one additional static item to that dynamic list.   I want to always have an option to filter for the supervisor position (jobs.Supervisor = "Yes"), currently, the Drop Down List only will show availalbe jobs based on being active or not.

Can I do this in my select query?

This is my datasrouce for my Drop Down List:

<asp:SqlDataSource ID="SqlDS2" runat="server"
ConnectionString="<%$ ConnectionStrings:JPConnString %>"          
SelectCommand=
"Select '--Select Position Title--' as Position_Title
Union All SELECT DISTINCT Jobs.Position_Title
FROM Job_Postings INNER JOIN Jobs ON Job_Postings.Jobs_FK = Jobs.Jobs_PK
WHERE (Job_Postings.Active_Flag = 'Active')
ORDER BY Jobs.Position_Title">
</asp:SqlDataSource>
<asp:DropDownList ID="ddPosition_Name"  
                DataTextField="Position_Title"
                AutoPostBack="true"             OnSelectedIndexChanged="Filter2DropDown_IndexChanged"
                OnPreRender="SetValue2"
                DataSourceID="SqlDS2" runat="server" style="font-size: x-small; font-family: Verdana"/>

Open in new window

0
Comment
Question by:ocdc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 34976928
Do you want to add the static value "--Select Position Title--" to your drop down list? You already have UNION with this value in the query. Is this value not displayed in the drop down box?
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 34977058
Change your query to

SELECT DISTINCT Jobs.Position_Title 
FROM Job_Postings INNER JOIN Jobs ON Job_Postings.Jobs_FK = Jobs.Jobs_PK 
WHERE (Job_Postings.Active_Flag = 'Active') 
ORDER BY Jobs.Position_Title

Open in new window


Then, in your code behind, do the following

ddPosition_Name.Items.Insert(0,new ListItem("--Select Position Title--"));

Open in new window

0
 

Author Comment

by:ocdc
ID: 34977059
Thanks for looking at this.  
No.  I need to add an additional item that actually filters when selected and where Supervisor ="yes"
Code behinde for drop down list.

    protected void Filter2DropDown_IndexChanged(object sender, EventArgs e)
    {
        DropDownList dd = (DropDownList)sender;
        if (dd.SelectedItem.Text != "ALL Position_Title")
        {
            if (filter.ContainsKey("Position_Title"))
            {
                filter["Position_Title"] = "='" + dd.SelectedItem.Text + "'";
            }
            else
            {
                filter.Add("Position_Title", "='" + dd.SelectedItem.Text + "'");
            }
        }
        else
        {
            filter.Remove("Position_Title");
        }
        ApplyGridFilter();
        GridView1.PageIndex = 0;
    }
jobs.JPG
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 14

Expert Comment

by:quizwedge
ID: 34978441
ocdc, you posted about the same time that I did. Does my solution work for you? I'm unclear by your post if you were responding just to Sharath_123 or to both of us.
0
 

Author Comment

by:ocdc
ID: 34985782
Quizwedge.  The my last posting was for Sharath_123.  (It looks like we posted around the same time)

I was looking at your code, and I don't see reference to the new item I was trying to add called "Supervisor".

Mabye I'm not explaining it correctly and I am more of an entry level programmer.
My drop down is being used for filtering.  The items of the drop down list are filled dynamically, based on a criteria:
Select '--Select Position Title--' as Position_Title Union All SELECT DISTINCT Jobs.Position_Title FROM Job_Postings INNER JOIN Jobs ON Job_Postings.Jobs_FK = Jobs.Jobs_PK
            WHERE (Job_Postings.Active_Flag = 'Active').

The '--Selct Postion TItle--' is really only a Title for the drop down list and does not actually filter, but items filled dynamically under that first item on the ddl which is '--Selct Postion TItle--' .  The filter uses the codebehind shown in my previous posting.  I need an additional item added to the ddl, that would filter for "Supervisor".  When I select the new item in the ddl called "Supervisor", I only want items to show in my gridview that have jobs.Supervisor = yes.  Does this make sense?

0
 
LVL 14

Accepted Solution

by:
quizwedge earned 500 total points
ID: 34986184
Updating the code I wrote earlier

Change your query to

SELECT DISTINCT Jobs.Position_Title 
FROM Job_Postings INNER JOIN Jobs ON Job_Postings.Jobs_FK = Jobs.Jobs_PK 
WHERE (Job_Postings.Active_Flag = 'Active') 
ORDER BY Jobs.Position_Title

Open in new window



Then, in your code behind, do the following

ddPosition_Name.Items.Insert(0,new ListItem("--Select Position Title--"));
ddPosition_Name.Items.Add(new ListItem("Supervisor"));

Open in new window


That will get the drop down set up correctly. I've modified your function below... I had to guess a little bit without having all of the code on the page.

protected void Filter2DropDown_IndexChanged(object sender, EventArgs e)
        {
            DropDownList dd = (DropDownList)sender;
            if (dd.SelectedItem.Text != "--Select Position Title--" && dd.SelectedItem.Text != "Supervisor")
            {
                filter.Remove("Supervisor");
                if (filter.ContainsKey("Position_Title"))
                {
                    filter["Position_Title"] = "='" + dd.SelectedItem.Text + "'";
                }
                else
                {
                    filter.Add("Position_Title", "='" + dd.SelectedItem.Text + "'");
                }
            }
            else if (dd.SelectedItem.Text == "Supervisor")
            {
                filter.Remove("Position_Title");
                if (filter.ContainsKey("Supervisor"))
                {
                    filter["Supervisor"] = "='yes'";
                }
                else
                {
                    filter.Add("Supervisor", "='yes'");
                }
            }
            else
            {
                filter.Remove("Position_Title");
                filter.Remove("Supervisor");
            }
            ApplyGridFilter();
            GridView1.PageIndex = 0;
        }

Open in new window


Let me know if you get any errors or have any problems.

0
 

Author Comment

by:ocdc
ID: 35002548
quizwedge:  Thanks for the help.
Worked Great.   I added the new ddl in my code here.

    protected void SetValue2(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        if (filter != null)
        {
            ddl.Items.Insert(0, new ListItem("--Select Position Title--"));
            ddl.Items.Add(new ListItem("Supervisor"));
            if (filter.ContainsKey("Position_Title"))
            {
                foreach (ListItem li in ddl.Items)
                {
                    if (li.Text == filter["Position_Title"].ToString().Substring(2, filter["Position_Title"].ToString().Length - 3))
                    {
                        li.Selected = true;
                        return;
                    }
                }
            }
        }
    }
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question