locked
Date range filtering on gridview RRS feed

  • Question

  • User-640323567 posted

    Hi,

    I have a gridview which needs to be filtered by date range. So I've place two ajax calendars with textboxes as start date and end date. I've placed ontextchanged event on those textboxes to bind the gridview everytime users change the date range.

    But, ontextchanged event triggers for both textboxes and gridview gets exactly double the rows of actual total rows.

    How do I implement this scenario? Pls help me, newbie here.

    Here is my code:

      <td>Date Range: 
                        <asp:TextBox ID="txtDate" runat="server" CausesValidation="true" AutoPostBack="true" onKeyPress="javascript: return false;" onPaste="javascript: return false;" OnTextChanged="txtDate_TextChanged">1/1/2016</asp:TextBox>
                        <ajaxToolkit:CalendarExtender ID="txtDate_CalendarExtender" runat="server" BehaviorID="txtDate_CalendarExtender" TargetControlID="txtDate"></ajaxToolkit:CalendarExtender>
    
                        &nbsp; To &nbsp;
    						<asp:TextBox ID="txtDate1" CausesValidation="true" runat="server" AutoPostBack="true" OnTextChanged="txtDate1_TextChanged" onKeyPress="javascript: return false;" onPaste="javascript: return false;">12/31/2016</asp:TextBox>
                        <ajaxToolkit:CalendarExtender ID="txtDate1_CalendarExtender" runat="server" BehaviorID="txtDate1_CalendarExtender" TargetControlID="txtDate1"></ajaxToolkit:CalendarExtender>
                    </td>
    
    <asp:GridView ID="gdv"
                    runat="server" /><Columns>
                        <asp:BoundField DataField="aaaa" HeaderText="aaaa" ItemStyle-VerticalAlign="Top" />
                        <asp:BoundField DataField="bbbbType" HeaderText="bbbbType" ItemStyle-VerticalAlign="Top" />
                        <asp:BoundField DataField="cccc" HeaderText="cccc" ItemStyle-VerticalAlign="Top"/>
                        <asp:BoundField DataField="ddddd" HeaderText="ddddd" ItemStyle-VerticalAlign="Top"/>
                             </Columns>
            </asp:GridView>
    =====================================================
    aspx.cs:
    =====================================================
        protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!IsPostBack)
                {
                    GetDataTable();
                }
            }
    
      protected void txtDate_TextChanged(object sender, EventArgs e)
            {
                TextBox txtDate = (TextBox)sender;
                ViewState["DateFilter"] = txtDate.Text;
    
                this.GetDataTable();
            }
    
            protected void txtDate1_TextChanged(object sender, EventArgs e)
            {
                TextBox txtDate1 = (TextBox)sender;
                ViewState["Date1Filter"] = txtDate1.Text;
    
                this.GetDataTable();
            }
    

    Thanks for any help.

    Friday, October 28, 2016 3:39 PM

Answers

  • User-691209617 posted

    Hi,

    Instead of Text change property add a button like search and on button click get the values of start date and end date and bind grid.

    Why you are not using  FilterExpression and pass the Text value of the TextBox in the FilterParameters of the SelectCommand that provides the data to the GridView

    <asp:sqldatasource id="articles" runat="server"
       connectionstring="<%$ ConnectionStrings:aspa %>" 
       selectcommand="SELECT title, url, added, updated FROM aspx_articles ORDER BY title" 
       filterexpression="title LIKE '%{0}%' or url LIKE '%{0}%'">
       <filterparameters>
          <asp:controlparameter controlid="searchBox" propertyname="Text" />
       </filterparameters>
    </asp:sqldatasource>

    Also try to follow the below mentioned thread.
    https://forums.asp.net/p/1034014/2904713.aspx

     Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 28, 2016 3:57 PM

All replies

  • User-691209617 posted

    Hi,

    Instead of Text change property add a button like search and on button click get the values of start date and end date and bind grid.

    Why you are not using  FilterExpression and pass the Text value of the TextBox in the FilterParameters of the SelectCommand that provides the data to the GridView

    <asp:sqldatasource id="articles" runat="server"
       connectionstring="<%$ ConnectionStrings:aspa %>" 
       selectcommand="SELECT title, url, added, updated FROM aspx_articles ORDER BY title" 
       filterexpression="title LIKE '%{0}%' or url LIKE '%{0}%'">
       <filterparameters>
          <asp:controlparameter controlid="searchBox" propertyname="Text" />
       </filterparameters>
    </asp:sqldatasource>

    Also try to follow the below mentioned thread.
    https://forums.asp.net/p/1034014/2904713.aspx

     Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 28, 2016 3:57 PM
  • User-271186128 posted

    Hi ASPbun,

    But, ontextchanged event triggers for both textboxes and gridview gets exactly double the rows of actual total rows.

    How do I implement this scenario? Pls help me, newbie here.

    From your description, do you mean you get the duplicate result (exactly double the rows of actual total rows)? If that is the case, based on your code, it seems that you are using GetDataTable method to bind GridView. I suggest you could check the relevant code, perhaps you populate the data into a DataTable twice, then, bind it to GridView.

    If possible, I suggest you could post the relevant code, it might be easier for us to find the real issue.

    Besides, here are some relevant articles about filter GridView, you can check them:

    http://www.aspforums.net/Threads/556439/Filter-GridView-based-on-Minimum-and-Maximum-Date-Ranges-in-ASPNet/

    http://geekswithblogs.net/dotNETvinz/archive/2009/05/07/filter-gridview-data-based-from-date-ranges.aspx

    Best regards,
    Dillion

    Friday, November 4, 2016 10:08 AM