Solved

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

Posted on 2011-02-24
7
839 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
  • 3
  • 3
7 Comments
 
LVL 40

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now