为什么我的控件参数搜索没有返回整数值?

时间:2011-08-08 10:09:28

标签: asp.net vb.net

我正在使用控制参数搜索网格视图,但在我的代码中,我只搜索文本而不是整数值。

如何使用控制参数搜索整数值?

我的代码是:

   <%@ Page Language="VB" AutoEventWireup="false" CodeFile="GridviewwithHighlightedSearch.aspx.vb"
Inherits="GridviewwithHighlightedSearch" MasterPageFile="~/Default.master" %>

  <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderMain" runat="Server">
  <style type="text/css">
      .highlight {text-decoration: none;color:black;background:yellow;}
  </style>
  <form id="form1" runat="server">
  <asp:ScriptManager ID="ScriptManager" runat="server" />
  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <h3>
            Gridview with Highlighted Search</h3>
        <div class="GridviewDiv">
            <p>
                Search for a person :
                <asp:TextBox ID="txtSearch" runat="server" />&nbsp;&nbsp;
                <asp:ImageButton ID="btnSearch" ImageUrl="images/searchbutton.png" runat="server"
                    Style="top: 5px; position: relative" />&nbsp;&nbsp;
                <asp:ImageButton ID="btnClear" ImageUrl="images/clearbutton.png" runat="server" Style="top: 5px;
                    position: relative" /><br />
                <br />
            </p>
            <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
                AllowSorting="true" DataSourceID="dsGridview" Width="540px" PageSize="10" CssClass="Gridview">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" ItemStyle-Width="40px"
                        ItemStyle-HorizontalAlign="Center" />
                    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
                        <ItemStyle Width="120px" HorizontalAlign="Left" />
                        <ItemTemplate>
                            <asp:Label ID="lblFirstname" Text='<%# HighlightText(Eval("FirstName")) %>' runat="server"
                                CssClass="TextField" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
                        <ItemStyle Width="120px" HorizontalAlign="Left" />
                        <ItemTemplate>
                            <asp:Label ID="lblLastname" Text='<%# HighlightText(Eval("LastName")) %>' runat="server"
                                CssClass="TextField" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department"
                        ItemStyle-Width="130px" />
                    <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location"
                        ItemStyle-Width="130px" />
                </Columns>
            </asp:GridView>
            </div>
    </ContentTemplate>
</asp:UpdatePanel>
<br />
<a href="GridviewwithHighlightedSearch.zip">Download Source Code</a> 
<asp:SqlDataSource ID="dsGridview" runat="server" ConnectionString="<%$ ConnectionStrings:EvonetConnectionString %>"
    SelectCommand="SELECT * FROM [T_Employees]" FilterExpression="firstname like '%{0}%' or lastname like '%{1}%'">
    <FilterParameters>
        <asp:ControlParameter Name="firstname" ControlID="txtSearch" PropertyName="Text" />
        <asp:ControlParameter Name="lastname" ControlID="txtSearch" PropertyName="Text" />
    </FilterParameters>
</asp:SqlDataSource>
</form>
</asp:Content>

1 个答案:

答案 0 :(得分:0)

您可以在过滤器中添加另一个OR条件以匹配整数列并添加相应的过滤器参数...这就是您的意思吗?

<asp:SqlDataSource
    ID="dsGridview"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:EvonetConnectionString %>"
    SelectCommand="SELECT * FROM [T_Employees]"
    FilterExpression="firstname like '%{0}%' or lastname like '%{1}%' or IntColumn = '{2}'">
    <FilterParameters>
        <asp:ControlParameter Name="firstname" ControlID="txtSearch" PropertyName="Text" />
        <asp:ControlParameter Name="lastname" ControlID="txtSearch" PropertyName="Text" />
        <asp:ControlParameter Name="IntColumn" ControlID="txtSearch" PropertyName="Text" />
    </FilterParameters>
</asp:SqlDataSource>

需要注意的重要一点是,您无法在like列上使用integer ...好吧,我猜不是先将它放在首位。