views:

2123

answers:

2

I have tried different examples to filter a gridview by dropdownlist, but is it possible in a button on_click event? Do i create something like a SelectedItem on each dropdown and have them added to the button event? Sorry to be so vague....I would like to have the dropdowns with a selectedvalue and then add them together to perform a filter based on the returning results.

Thanks

A: 

Your question seems a bit vague, but if I'm interpreting correctly you want a couple dropdownlists and a button. The filtering shouldn't happen until the button is pressed.

Yes, you should be able to put code in the button_click event that references the SelectedItem(s) or SelectedValue(s) of the dropdowns and then builds and expression which can be used to set the RowFilter of the GridView.

ahockley
A: 

Hi, I have a similar question. Here is my code. Please let me know what needs to be corrected so multiple dropdown lists can be used together (in any combination) or individually. For now they all have to be selected in order for filtering to work. Thanks

Surname Search:
  Given Names Search:
   

" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM dbo.newMembersAll WHERE dbo.newMembersAll.Worksite=@Worksite AND dbo.newMembersAll.Chapter=@Chapter AND dbo.newMembersAll.BUName=@BUName AND dbo.newMembersAll.CommonName=@CommonName AND dbo.newMembersAll.Occupation=@Occupation ORDER BY dbo.newMembersAll.Surname, dbo.newMembersAll.GivenNames" Runat="server" OnSelecting="srcMembers_Selecting">
Occupation " ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT Name AS Occupation FROM Occupations ORDER BY Name" Runat="server" /> Barg.Unit " ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT BUName FROM Contracts WHERE ContractID '00' ORDER BY BUName" Runat="server" />
Worksite " ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT Name AS Worksite FROM Worksites ORDER BY Name" Runat="server" /> Employer " ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT CommonName FROM Employers ORDER BY CommonName" Runat="server" />
Chapter " ProviderName="System.Data.SqlClient" SelectCommand="SELECT DISTINCT Name AS Chapter FROM Chapters WHERE ChapterID 170 ORDER BY Name" Runat="server" /> Region


protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ddlOccupation.DataSource = srcOccupationCategories.Select(DataSourceSelectArguments.Empty); ddlOccupation.DataBind(); ddlOccupation.Items.Insert(0, "(All Occupations)"); ddlOccupation.SelectedIndex = 0;

        ddlBU.DataSource = srcBUCategories.Select(DataSourceSelectArguments.Empty);
        ddlBU.DataBind();
        ddlBU.Items.Insert(0, "(All Bargaining Units)");
        ddlBU.SelectedIndex = 0;

        ddlWorksite.DataSource = srcWorksiteCategories.Select(DataSourceSelectArguments.Empty);
        ddlWorksite.DataBind();
        ddlWorksite.Items.Insert(0, "(All Worksites)");
        ddlWorksite.SelectedIndex = 0;

        ddlEmployer.DataSource = srcEmployerCategories.Select(DataSourceSelectArguments.Empty);
        ddlEmployer.DataBind();
        ddlEmployer.Items.Insert(0, "(All Employers)");
        ddlEmployer.SelectedIndex = 0;

        ddlChapter.DataSource = srcChapterCategories.Select(DataSourceSelectArguments.Empty);
        ddlChapter.DataBind();
        ddlChapter.Items.Insert(0, "(All Chapters)");
        ddlChapter.SelectedIndex = 0;
    }


}

protected void srcMembers_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    if ((string)e.Command.Parameters["@Occupation"].Value == "(All Occupations)")
    {
        e.Command.CommandText = "SELECT * FROM dbo.newMembersAll ORDER BY dbo.newMembersAll.Surname, dbo.newMembersAll.GivenNames";
    }
    if ((string)e.Command.Parameters["@Worksite"].Value == "(All Worksites)")
    {
        e.Command.CommandText = "SELECT * FROM dbo.newMembersAll ORDER BY dbo.newMembersAll.Surname, dbo.newMembersAll.GivenNames";
    }
    if ((string)e.Command.Parameters["@Chapter"].Value == "(All Chapters)")
    {
        e.Command.CommandText = "SELECT * FROM dbo.newMembersAll ORDER BY dbo.newMembersAll.Surname, dbo.newMembersAll.GivenNames";
    }
    if ((string)e.Command.Parameters["@CommonName"].Value == "(All Employers)")
    {
        e.Command.CommandText = "SELECT * FROM dbo.newMembersAll ORDER BY dbo.newMembersAll.Surname, dbo.newMembersAll.GivenNames";
    }
    if ((string)e.Command.Parameters["@BUName"].Value == "(All Bargaining Units)")
    {
        e.Command.CommandText = "SELECT * FROM dbo.newMembersAll ORDER BY dbo.newMembersAll.Surname, dbo.newMembersAll.GivenNames";
    }
ana