Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-24
7
Medium Priority
?
849 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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