3 Replies - 4376 Views - Last Post: 22 January 2014 - 05:50 AM

#1 Emwat Oon  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 07-January 12

GridView, Postback Issue with Filter?

Posted 13 January 2014 - 02:09 PM

I'm trying to apply a filter to my gridview using someone's tutorial, but I've come up short and I do not know why.

There would be a dropdown menu in the headertemplate and supposedly changing the selected item would apply the filter to the gridview.

However, when I do so, nothing changes to the gridview, and the dropdown menu may or may not return to its default value (some things I've tried makes it return to its default value, some things I've tried does not affect the dropdown menu).

I've thrown breakpoints everywhere and they all hit appropriately.

This is the code in question.
    private void BindGridView()
    {
        string query = @"This space was intentionally left blank";
        using (iDB2Connection conn = new iDB2Connection(connect))
        {
            using (iDB2Command cmd = new iDB2Command(query, conn))
            {
                conn.Open();
                iDB2DataAdapter da = new iDB2DataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];
                GridView1.DataSource = ds;
                //iDB2DataReader reader;
                //reader = cmd.ExecuteReader();
                //GridView1.DataSource = reader;
                GridView1.DataBind();
                GridView1.GridLines = GridLines.Horizontal;
                DropDownList ddlTerminal = (DropDownList)GridView1.HeaderRow.FindControl("ddlTerminal");
                this.BindTerminalList(ddlTerminal);
            }
        }







    protected void TerminalChanged(object sender, EventArgs e)
    {
        DropDownList ddlTerminal = (DropDownList)sender;
        ViewState["Filter"] = ddlTerminal.SelectedValue;
        this.BindGridView();
    }


I want to think it's related to postback, but I'm not necessarily sure. I've asked about it and I'm told the gridview populates dynamically, which leads to my datatable being null upon postback.

This is the asp.net in question.
                    <HeaderTemplate>
                        Terminal:
                        <asp:DropDownList ID="ddlTerminal" runat="server"
                        AppendDataBoundItems = "true"
                        AutoPostBack="true"
                        onselectedIndexChanged="TerminalChanged"
                        >
                            <asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
                        </asp:DropDownList>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("VUCTID") %>
                    </ItemTemplate>
                </asp:TemplateField>



Is This A Good Question/Topic? 0
  • +

Replies To: GridView, Postback Issue with Filter?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3467
  • View blog
  • Posts: 11,768
  • Joined: 12-December 12

Re: GridView, Postback Issue with Filter?

Posted 13 January 2014 - 02:52 PM

I know nothing about ASP.NET but there is a similar question here at SO which may help.

My guess is that your call to BindGridView() and it's refilling of the DataSet is discarding the filter. (Perhaps the SelectedValue can be passed to BindGridView as a parameter?)

Sorry that I can't help further, maybe someone else ;)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3467
  • View blog
  • Posts: 11,768
  • Joined: 12-December 12

Re: GridView, Postback Issue with Filter?

Posted 13 January 2014 - 03:04 PM

Actually, this only stores a value for persistence:
ViewState["Filter"] = ddlTerminal.SelectedValue;

there is nothing special about the key "Filter". You need to refer to this value when you re-populate the GridView. Something like this:
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());

ASPSnippets tutorial
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: GridView, Postback Issue with Filter?

Posted 22 January 2014 - 05:50 AM

andrewsw has the gist of it. You're creating the "Filter" viewstate value, but you're never actually using it to filter the data. Really you don't even need the ViewState["Filter"]. You just need to get the value from the dropdown in your bindgridview method and use the value to filter the gridview. You shouldn't need to reset the dropdown in that method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1