Link to home
Start Free TrialLog in
Avatar of PSERS BIT
PSERS BITFlag for United States of America

asked on

Gridview filter dropdowns empty after postback

Hi everyone,

I am having trouble with dropdown list being empty after a postback.  I am attempting to create a gridview with multiple filters in the headers using dropdowns.  When I select a dropdown and the grid filters, I would like the other dropdowns to only contain values that are left in the grid.  For example, if I choose a state region, I would like the county dropdown to only contain the counties in that region.

The code I have hear is for a very simple test application.  Please take a look and let me know if you have any suggestions.  The filtering part works correctly when I bind the grid on the OnSelectedIndexChanged but the dropdowns will all be empty.  If I do not bind the grid on the OnSelectedIndexChanged, the dropdowns work perfectly.  I just cannot get both to work at the same time.

Thank you,
Eric

[HTML]
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id"></asp:BoundField>
            <asp:BoundField DataField="date" HeaderText="date" SortExpression="date"></asp:BoundField>
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone"></asp:BoundField>
            <asp:TemplateField HeaderText="floor_area" SortExpression="floor_area">
                <HeaderTemplate>
                    <asp:DropDownList ID="ddlfloor" runat="server" AutoPostBack="true" OnSelectedIndexChanged="FilterDropDown_IndexChanged" AppendDataBoundItems="true"></asp:DropDownList>
                </HeaderTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" Text='<%# Bind("floor_area") %>' ID="TextBox1"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" Text='<%# Bind("floor_area") %>' ID="Label1"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="room" SortExpression="room">
                <HeaderTemplate>
                    <asp:DropDownList ID="room" runat="server" AutoPostBack="true"></asp:DropDownList>
                </HeaderTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" Text='<%# Bind("room") %>' ID="TextBox2"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" Text='<%# Bind("room") %>' ID="Label2"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="description" HeaderText="description" SortExpression="description"></asp:BoundField>
            <asp:BoundField DataField="notes" HeaderText="notes" SortExpression="notes"></asp:BoundField>
            <asp:TemplateField HeaderText="received" SortExpression="received">
                <HeaderTemplate>
                    <asp:DropDownList ID="received" runat="server" AutoPostBack="true"></asp:DropDownList>
                </HeaderTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" Text='<%# Bind("received") %>' ID="TextBox3"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" Text='<%# Bind("received") %>' ID="Label3"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="completed" SortExpression="completed">
                <HeaderTemplate>
                    <asp:DropDownList ID="completed" runat="server" AutoPostBack="true"></asp:DropDownList>
                </HeaderTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" Text='<%# Bind("completed") %>' ID="TextBox4"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label runat="server" Text='<%# Bind("completed") %>' ID="Label4"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            
            <asp:BoundField DataField="classification" HeaderText="classification" SortExpression="classification"></asp:BoundField>
        </Columns>
    </asp:GridView>
[/HTML]

[c#]
public partial class WebForm4 : System.Web.UI.Page
    {

        public SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                {
                    DBConnection.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM [BuildingMaint_2]", DBConnection))

                    {

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {

                            if (reader != null)
                            {
                                cmd.CommandType = CommandType.Text;
                                cmd.Connection = DBConnection;
                                GridView1.Visible = true;
                                GridView1.DataSource = reader;
                                GridView1.DataBind();

                            }
                        }
                    }

                }
            }
        }


        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!IsPostBack)
            {

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT floor_area FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {
                                    DropDownList ddlfloor = (e.Row.FindControl("ddlfloor") as DropDownList);

                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;
                                    ddlfloor.DataSource = reader;
                                    ddlfloor.DataTextField = "floor_area";
                                    ddlfloor.DataValueField = "floor_area";
                                    ddlfloor.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlfloor.Items.Insert(0, new ListItem("Please select"));

                                }
                            }
                        }
                    }
                }

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT room FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {

                                    DropDownList ddlroom = (e.Row.FindControl("room") as DropDownList);


                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;
                                    ddlroom.DataSource = reader;
                                    ddlroom.DataTextField = "room";
                                    ddlroom.DataValueField = "room";
                                    ddlroom.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlroom.Items.Insert(0, new ListItem("Please select"));

                                }
                            }
                        }
                    }
                }

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT received FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {
                                    DropDownList ddlreceived = (e.Row.FindControl("received") as DropDownList);
                                    ddlreceived.DataSource = reader;
                                    ddlreceived.DataTextField = "received";
                                    ddlreceived.DataValueField = "received";
                                    ddlreceived.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlreceived.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                }

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT completed FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {
                                    DropDownList ddlcompleted = (e.Row.FindControl("completed") as DropDownList);
                                    ddlcompleted.DataSource = reader;
                                    ddlcompleted.DataTextField = "completed";
                                    ddlcompleted.DataValueField = "completed";
                                    ddlcompleted.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlcompleted.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                }
                else
                {

                }
            }
        }

        protected void FilterDropDown_IndexChanged(object sender, EventArgs e)

        {


            GridViewRow header = GridView1.HeaderRow;
           
                
                
                     DropDownList ddlfloor = header.FindControl("ddlfloor") as DropDownList;
                    DropDownList ddlroom = header.FindControl("room") as DropDownList;
                    DropDownList ddlreceived = header.FindControl("received") as DropDownList;
                    string floor = ddlfloor.SelectedValue;

            using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
            {
                DBConnection.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT floor_area FROM BuildingMaint_2", DBConnection))

                {

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {

                        if (reader != null)
                        {


                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = DBConnection;

                            ddlfloor.DataSource = reader;
                            ddlfloor.DataTextField = "floor_area";
                            ddlfloor.DataValueField = "floor_area";
                            ddlfloor.DataBind();
                            ddlfloor.Items.Insert(0, new ListItem("Please select"));
                            ddlfloor.SelectedValue = floor.ToString();
                        }
                    }
                }
           }

            using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT room FROM BuildingMaint_2 WHERE floor_area = '" + floor + "'", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {


                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;
                                    
                                    ddlroom.DataSource = reader;
                                    ddlroom.DataTextField = "room";
                                    ddlroom.DataValueField = "room";
                                    ddlroom.DataBind();
                                    ddlroom.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT received FROM BuildingMaint_2 WHERE floor_area = '" + floor + "'", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {


                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;

                                    ddlreceived.DataSource = reader;
                                    ddlreceived.DataTextField = "received";
                                    ddlreceived.DataValueField = "received";
                                    ddlreceived.DataBind();
                                    ddlreceived.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                   {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT * FROM BuildingMaint_2 WHERE floor_area = '" + floor + "'", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {


                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = DBConnection;
                            GridView1.Visible = true;
                            GridView1.DataSource = reader;
                            GridView1.EnableViewState = false;
                            GridView1.DataBind();


                        }
                            }
                        }
                    }
                

            }
            
        } 
    
}
[/c#]

Open in new window

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Eric,

Kindly check that you are not re-binding the dropdowns in Postback. If you can post your code behind I can quickly pinpoint where you need to make changes.

Regards,
Chinmay.
Avatar of PSERS BIT

ASKER

Hi Chinmay,

Here is the code.  It was posted above, but thought I would pull it out so it was easier to see.

[c#]
public partial class WebForm4 : System.Web.UI.Page
    {

        public SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                {
                    DBConnection.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM [BuildingMaint_2]", DBConnection))

                    {

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {

                            if (reader != null)
                            {
                                cmd.CommandType = CommandType.Text;
                                cmd.Connection = DBConnection;
                                GridView1.Visible = true;
                                GridView1.DataSource = reader;
                                GridView1.DataBind();

                            }
                        }
                    }

                }
            }
        }


        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (!IsPostBack)
            {

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT floor_area FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {
                                    DropDownList ddlfloor = (e.Row.FindControl("ddlfloor") as DropDownList);

                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;
                                    ddlfloor.DataSource = reader;
                                    ddlfloor.DataTextField = "floor_area";
                                    ddlfloor.DataValueField = "floor_area";
                                    ddlfloor.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlfloor.Items.Insert(0, new ListItem("Please select"));

                                }
                            }
                        }
                    }
                }

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT room FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {

                                    DropDownList ddlroom = (e.Row.FindControl("room") as DropDownList);


                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;
                                    ddlroom.DataSource = reader;
                                    ddlroom.DataTextField = "room";
                                    ddlroom.DataValueField = "room";
                                    ddlroom.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlroom.Items.Insert(0, new ListItem("Please select"));

                                }
                            }
                        }
                    }
                }

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT received FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {
                                    DropDownList ddlreceived = (e.Row.FindControl("received") as DropDownList);
                                    ddlreceived.DataSource = reader;
                                    ddlreceived.DataTextField = "received";
                                    ddlreceived.DataValueField = "received";
                                    ddlreceived.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlreceived.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                }

                if (e.Row.RowType == DataControlRowType.Header)
                {
                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT completed FROM BuildingMaint_2", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {
                                    DropDownList ddlcompleted = (e.Row.FindControl("completed") as DropDownList);
                                    ddlcompleted.DataSource = reader;
                                    ddlcompleted.DataTextField = "completed";
                                    ddlcompleted.DataValueField = "completed";
                                    ddlcompleted.DataBind();

                                    //Add Default Item in the DropDownList
                                    ddlcompleted.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                }
                else
                {

                }
            }
        }

        protected void FilterDropDown_IndexChanged(object sender, EventArgs e)

        {


            GridViewRow header = GridView1.HeaderRow;
           
               
               
                     DropDownList ddlfloor = header.FindControl("ddlfloor") as DropDownList;
                    DropDownList ddlroom = header.FindControl("room") as DropDownList;
                    DropDownList ddlreceived = header.FindControl("received") as DropDownList;
                    string floor = ddlfloor.SelectedValue;

            using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
            {
                DBConnection.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT floor_area FROM BuildingMaint_2", DBConnection))

                {

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {

                        if (reader != null)
                        {


                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = DBConnection;

                            ddlfloor.DataSource = reader;
                            ddlfloor.DataTextField = "floor_area";
                            ddlfloor.DataValueField = "floor_area";
                            ddlfloor.DataBind();
                            ddlfloor.Items.Insert(0, new ListItem("Please select"));
                            ddlfloor.SelectedValue = floor.ToString();
                        }
                    }
                }
           }

            using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT room FROM BuildingMaint_2 WHERE floor_area = '" + floor + "'", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {


                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;
                                   
                                    ddlroom.DataSource = reader;
                                    ddlroom.DataTextField = "room";
                                    ddlroom.DataValueField = "room";
                                    ddlroom.DataBind();
                                    ddlroom.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                    {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT received FROM BuildingMaint_2 WHERE floor_area = '" + floor + "'", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {


                                    cmd.CommandType = CommandType.Text;
                                    cmd.Connection = DBConnection;

                                    ddlreceived.DataSource = reader;
                                    ddlreceived.DataTextField = "received";
                                    ddlreceived.DataValueField = "received";
                                    ddlreceived.DataBind();
                                    ddlreceived.Items.Insert(0, new ListItem("Please select"));
                                }
                            }
                        }
                    }

                    using (SqlConnection DBConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MemberWebConnectionString"].ConnectionString))
                   {
                        DBConnection.Open();
                        using (SqlCommand cmd = new SqlCommand("SELECT * FROM BuildingMaint_2 WHERE floor_area = '" + floor + "'", DBConnection))

                        {

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {

                                if (reader != null)
                                {


                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = DBConnection;
                            GridView1.Visible = true;
                            GridView1.DataSource = reader;
                            GridView1.EnableViewState = false;
                            GridView1.DataBind();


                        }
                            }
                        }
                    }
               

            }
           
        }
   
}
[/c#]
Hi Eric,

Sorry for the delay and confusion in the code - I believe I responded to your question from mobile where
 tag does not work.

Now, looking at your code I wonder, can you try to step through the OnSelectedindexChanged method? you are repopulating the combos but i think you might be getting 0 results from the database.

Can you check in debugger, whether you are getting a right value on this line: 

[code]string floor = ddlfloor.SelectedValue;

Open in new window


Regards,
Chinmay.
ASKER CERTIFIED SOLUTION
Avatar of PSERS BIT
PSERS BIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial