• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 863
  • Last Modified:

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

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
ocdc
Asked:
ocdc
  • 3
  • 3
1 Solution
 
SharathData EngineerCommented:
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
 
quizwedgeCommented:
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
 
ocdcAuthor Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
quizwedgeCommented:
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
 
ocdcAuthor Commented:
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
 
quizwedgeCommented:
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
 
ocdcAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now