通过文本框过滤Gridview

时间:2013-05-07 10:05:55

标签: gridview

这是包含用于将数据输入到列

的列名和文本框的表
<table border="1">
                <tr>
 <th>EmpId</th>
<th>EmpName</th>
 <th>EmpSalary</th>
 <th>EmpPhone</th>
<th>EmpAddress</th>
 </tr>
 <tr>
<td>
<asp:TextBox ID="TxtEmpId" runat="server" Width="134px" AutoPostBack="true"></asp:TextBox></td>
<td>
<asp:TextBox ID="TxtEmpName" runat="server" Width="121px"></asp:TextBox></td>
<td>
 <asp:TextBox ID="TxtEmpSalary" runat="server" Width="128px"></asp:TextBox></td>
  <td>
<asp:TextBox ID="TxtEmpPhone" runat="server" Width="117px"></asp:TextBox></td>
<td>
  <asp:TextBox ID="TxtEmpAddress" runat="server" Width="120px"></asp:TextBox></td>
</tr>
</table>

这是一个包含列

的网格
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"               DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False" ShowHeader="false"
CssClass="GridViewStyle" Width="650px" DataKeyNames="EmpId" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
 <Columns>
  <asp:BoundField DataField="EmpId" HeaderText="EmpId" ItemStyle-Width="200px" InsertVisible="False" ReadOnly="True" SortExpression="EmpId" >
  <ItemStyle Width="200px" />
 </asp:BoundField>
  <asp:BoundField DataField="EmpName" HeaderText="EmpName" ItemStyle-Width="125px" SortExpression="EmpName" >
  <ItemStyle Width="125px" />
</asp:BoundField>
<asp:BoundField DataField="EmpSalary" HeaderText="EmpSalary" ItemStyle-Width="125px" SortExpression="EmpSalary" >
  <ItemStyle Width="125px" />
</asp:BoundField>
 <asp:BoundField DataField="EmpPhone" HeaderText="EmpPhone" ItemStyle-Width="125px" SortExpression="EmpPhone" >
  <ItemStyle Width="125px" />
</asp:BoundField>
 <asp:BoundField DataField="EmpAddress" HeaderText="EmpAddress" ItemStyle-Width="125px" SortExpression="EmpAddress" >
  <ItemStyle Width="125px" />
 </asp:BoundField>
 </Columns>
 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
 <SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
 <SortedDescendingCellStyle BackColor="#F6F0C0" />
  <SortedDescendingHeaderStyle BackColor="#7E0000" />
 </asp:GridView>

包含testingdb数据和过滤器参数的sqldatasourc 这里如果我设置ConvertEmptystringToNull=false它显示错误,因为输入字符串格式不正确, 但是使用ConvertEmptyStringToNull="True"它正在工作意味着gridview填充了浏览器中的数据

  <asp:SqlDataSource ID="sqlDataSourceGridView" runat="server" ConnectionString="<%$ ConnectionStrings:TestingDBConnectionString %>" SelectCommand="SELECT * FROM [Employees]"
                FilterExpression="[EmpName] like '{1}%' and [EmpSalary] like '{2}%' and [EmpPhone] like '{3}%' [EmpAddress] like '{4}%'"  >
 <FilterParameters>
  <asp:ControlParameter ControlID="TxtEmpId" Name="EmpId" DefaultValue="" 
   PropertyName="Text" Type="Int32" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpName" Name="EmpName" DefaultValue="" 
 PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpSalary" Name="EmpSalary" DefaultValue=""
 PropertyName="Text" Type="Decimal" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpPhone" Name="EmpPhone" DefaultValue=""
    PropertyName="Text" Type="Int64" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpAddress" Name="EmpAddress" DefaultValue="" 
  PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" />
 </FilterParameters>
</asp:SqlDataSource>

使用上面的代码数据绑定到带有表文本框的gridview 当我在文本框中输入文本时,gridview数据不会填满。 我想通过在文本框中输入数据来过滤我的gridview。 请解决我的问题

2 个答案:

答案 0 :(得分:1)

使用控制参数来控制文本字段。简单的例子:

查询

Select * from tbl_1 WHERE (([Titel] LIKE '%' + @Titel + '%')

控制

 <asp:ControlParameter ControlID="txtTitel" DefaultValue="*" Name="Titel" 
        PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />

答案 1 :(得分:0)

设置属性AutopostBack on Filter = True用于过滤记录。 试试这个.......