locked
Filtering data into gridview from a textbox RRS feed

  • Question

  • User17798125 posted

    Hi forum, 

    I am implementing a very easy filter to a grid view, in conjunction with a SQL data. I have a DropDownList and a Textbox, with the first I do not have any issue, but the second is my headache. I though that the same method applied for the first will be functional to the second, but ti is not, which is, typing the info into the textbox and then out of it to activate the autopostback=true, as I said, it does not work. Below I leave my code. Is in HTML no code behind (vb.net). Maybe it is not the best way to filter data, but it works and the project is small to medium.

    My question is, what I am doing wrong? What could be the best way to do it?   Thanks in advance!

     <asp:GridView ID="Gridview2" runat="server" Style="margin-left:21px" ShowHeader="false" ShowFooter="false" DataSourceID="SqlDataSource2" AutoGenerateColumns="False" AllowPaging="True" Width="151px" PageSize="1" PagerSettings-Mode="NextPrevious" PagerSettings-NextPageImageUrl="~/cupbut2.png" PagerStyle-ForeColor="#FFFFFF" PagerSettings-PreviousPageImageUrl="~/cupbut1.png" PagerStyle-BackColor="#FFFFFF" BorderWidth="0px" AlternatingRowStyle-BorderWidth="0px" HeaderStyle-BorderColor="#FFFFFF" RowStyle-BorderColor="#FFFFFF" FooterStyle-BorderWidth="0px" PagerStyle-BorderWidth="0px" PagerStyle-BorderColor="Transparent" BackColor="#FFFFFF" EditRowStyle-BorderWidth="0px" EmptyDataRowStyle-BorderWidth="0px" HeaderStyle-BorderWidth="0px" RowStyle-BorderWidth="0px" RowStyle-BackColor="#FFFFFF" SelectedRowStyle-BorderWidth="0px" SelectedRowStyle-BackColor="#FFFFFF">
                             <Columns>                                                          
                                  <asp:TemplateField ShowHeader="false" ControlStyle-BorderColor="#FFFFFF" ControlStyle-BackColor="#FFFFFF" ControlStyle-BorderWidth="0px" FooterStyle-BackColor="#FFFFFF" FooterStyle-BorderWidth="0px" HeaderStyle-BorderWidth="0px" HeaderStyle-BackColor="#FFFFFF" ItemStyle-BorderWidth="1px" ItemStyle-BackColor="#FFFFFF" ItemStyle-BorderColor="#FFFFFF">
                                    <ItemTemplate>
                                      <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageID", "Handler2.ashx?ImID={0}")%>' Height="215px" Width="245px" BorderWidth="0px" BackColor="#FFFFFF" />
                                        <img src="shacup.png" alt="" style="top:2px; left:27px" /> 
                                    </ItemTemplate>
                                  </asp:TemplateField>                           
                             </Columns>
                      </asp:GridView>  
    
    
    
    <br /><br /><br />
    
    
    
    
                                  <asp:DropDownList ID="ddrubro" AutoPostBack="true" ForeColor="#465564" BackColor="#FFFFFF" Font-Names="Arial" style="border:0px" DataValueField="rub" DataSourceID="dspoprubro" runat="server" Width="130px" Font-Size="14px" AppendDataBoundItems="true">
                                  <asp:ListItem Text="Rubro" Value="%"></asp:ListItem>
                                  </asp:DropDownList>
                      
                              <br />
                           
                                 <asp:textbox ID="idd" runat="server" AutoPostBack="true" width="151px" Height="25px" Font-Size="14px" BorderColor="#FFFFFF" ForeColor="#465564" Font-Bold="true" BorderStyle="Solid" style="border-bottom:solid 1px #AAAAAA"></asp:textbox><br />
                                 <asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="idd" WatermarkText="Write ID"></asp:TextBoxWatermarkExtender>                     
                                                  
    
    
    
    
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT * FROM [cupones]" FilterExpression="rub like '{0}% and idne like '{1}%'">
                       <FilterParameters>
                           <asp:ControlParameter Name="rub" ControlID="ddrubro" PropertyName="SelectedValue" />
                           <asp:ControlParameter Name="idne" ControlID="idd" /> 
                       </FilterParameters>                 
                     </asp:SqlDataSource>
                     
                     <asp:SqlDataSource ID="dspoprubro" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT DISTINCT rub from [cupones]"></asp:SqlDataSource>
                     
                     <asp:SqlDataSource ID="iddc" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT DISTINCT idne from [cupones]"></asp:SqlDataSource>

    Wednesday, October 24, 2018 10:05 PM

Answers

  • User2103319870 posted

    Jonsey10

    but ti is not, which is, typing the info into the textbox and then out of it to activate the autopostback=true, as I said, it does not work

    Change the query like below 

     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT * FROM [cupones]" FilterExpression="rub like '%{0}%' AND idne like '%{1}%'">
                       <FilterParameters>
                           <asp:ControlParameter Name="rub" ControlID="ddrubro" PropertyName="SelectedValue" />
                           <asp:ControlParameter Name="idne" ControlID="idd" /> 
                       </FilterParameters>                 
                     </asp:SqlDataSource>

    Demo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 24, 2018 11:01 PM
  • User-1716253493 posted

    Do you want stop filter when textbox empty? Use default value with some text that is not exist in the database

    <asp:ControlParameter Name="idne" ControlID="idd" PropertyName="Text" DefaultValue="Nothing to show" /> 

    For the ddl set the first item value like above

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 25, 2018 6:53 AM

All replies

  • User2103319870 posted

    Jonsey10

    but ti is not, which is, typing the info into the textbox and then out of it to activate the autopostback=true, as I said, it does not work

    Change the query like below 

     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT * FROM [cupones]" FilterExpression="rub like '%{0}%' AND idne like '%{1}%'">
                       <FilterParameters>
                           <asp:ControlParameter Name="rub" ControlID="ddrubro" PropertyName="SelectedValue" />
                           <asp:ControlParameter Name="idne" ControlID="idd" /> 
                       </FilterParameters>                 
                     </asp:SqlDataSource>

    Demo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 24, 2018 11:01 PM
  • User-1716253493 posted

    Try this

    <asp:ControlParameter Name="rub" ControlID="ddrubro" PropertyName="SelectedValue" />
    <asp:ControlParameter Name="idne" ControlID="idd" PropertyName="Text" /> 

    Thursday, October 25, 2018 12:53 AM
  • User17798125 posted

    Thank you so much!

    It works perfectly, if I write into the textbox the filter works perfect, and in I use the dropdownlist works as a second filter.
    just one more doubt, how I can make it work independently, I mean, filter with the textbox or the dropdownlist?

    I tried this but no success, and works fine, but is I need the DDL make the filter the TB need to have something in it, any letter or number. Do I prefer is use the DDL  as a filter without typing something into the TB and vice versa, or something. If there is nothing could do, I leave it like that. 

    Thanks in advance!

     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT * FROM [cupones]" FilterExpression="rub = '{0}' OR idne = '{1}'">                                                        
    
                       <FilterParameters>
                           <asp:ControlParameter Name="rub" ControlID="ddrubro" PropertyName="SelectedValue" /> 
                           <asp:ControlParameter Name="idne" ControlID="idd" PropertyName="Text"  />
                       </FilterParameters>                 
                     </asp:SqlDataSource>
                     
                     <asp:SqlDataSource ID="dspoprubro" runat="server" ConnectionString="<%$ ConnectionStrings:con %>" 
                     SelectCommand="SELECT DISTINCT rub from [cupones]"></asp:SqlDataSource>    



    Thursday, October 25, 2018 2:17 AM
  • User-1716253493 posted

    Do you want stop filter when textbox empty? Use default value with some text that is not exist in the database

    <asp:ControlParameter Name="idne" ControlID="idd" PropertyName="Text" DefaultValue="Nothing to show" /> 

    For the ddl set the first item value like above

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 25, 2018 6:53 AM
  • User17798125 posted

    Thanks both!
    Is amazing how the simple things are those difficult ones to see :)

    Friday, October 26, 2018 12:15 AM