1

这是带有列名和文本框的表格,用于将数据输入到列中

<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 数据和过滤器参数的 sqldatasource 如果我将其设置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 数据没有填充。我想通过在文本框中输入数据来过滤我的网格视图。请解决我的问题

4

2 回答 2

1

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

查询

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

去控制

 <asp:ControlParameter ControlID="txtTitel" DefaultValue="*" Name="Titel" 
        PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />
于 2013-05-07T10:11:32.210 回答
0

设置属性AutopostBack on Filter = True过滤您的记录。尝试这个.......

于 2013-05-07T10:10:25.990 回答