3

我在 asp.net 中工作,我有三个文本框(名字、姓氏、中间名缩写)。有一个列表视图,根据输入到文本框中的信息显示内容。我试图只需要姓氏文本框来处理信息。我已经使用以下代码将 sql 数据源连接到列表视图:

    <asp:TabPanel runat="server" HeaderText="Information" ID="TabPanel">             
     <ContentTemplate> 

         <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
             ConnectionString="<%$ Name of the database %>" 
             SelectCommand="SELECT [UserID], [UserSuffix], [UserAddressLine1], [UserAddressLine2], [UserCity], 
             [UserState], [UserZipCode], [UserPhoneNumber], [UserEmail] FROM [AllUsers]
             WHERE (([UserLastName] = @UserLastName) OR ([UserFirstName] = @UserFirstName) 
             OR ([UserMiddleInitial] = @UserMiddleInitial))">
             <SelectParameters>
                 <asp:ControlParameter ControlID="txtUILastName" Name="UserLastName" 
                     PropertyName="Text" />
                 <asp:ControlParameter ControlID="txtUIFirstName" Name="UserFirstName" 
                     PropertyName="Text" />
                 <asp:ControlParameter ControlID="txtUIMI" Name="UserMiddleInitial" 
                     PropertyName="Text" />
             </SelectParameters>
         </asp:SqlDataSource>

      <table id = "USERINF">
          <tr>
           <td width="196px" class="style8">
             <asp:Label ID="lblFirstName" runat="server" Text="Please enter First Name:"></asp:Label>
           </td>
           <td class="style6">

            <asp:TextBox ID="txtUIFirstName" runat="server" MaxLength="20"></asp:TextBox>


           </td>
          </tr>
          <tr>
           <td width="196px" class="style8">
            <asp:Label ID="lblLastName" runat="server" Text="Please enter Last Name:"></asp:Label>

           </td>
           <td class="style6">
             <asp:TextBox ID="txtUILastName" runat="server" MaxLength="20"></asp:TextBox> 

            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
             ControlToValidate = "txtUILastName" ValidationExpression = "[a-zA-Z ]{1,20}"
             ForeColor="Red" ValidationGroup="UserInf"
             ErrorMessage="First Name must be only a-z,A-Z characters!" Display="Dynamic"></asp:RegularExpressionValidator>

             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                   ControlToValidate = "txtUILastName" ErrorMessage="Please enter Last Name!" 
                   ForeColor="Red" ValidationGroup="UserInf" Display="Dynamic"></asp:RequiredFieldValidator>
           </td> 
          </tr>
          <tr>
           <td width="196px" class="style8">
            <asp:Label ID="lblMI" runat="server" Text="Please enter Middle Initials:"></asp:Label>
           </td>
           <td class="style6">
            <asp:TextBox ID="txtUIMI" runat="server" MaxLength="1" Width="30px" style="text-align: center;
                 margin-left:2px"></asp:TextBox> 

           </td> 
          </tr>
          <tr>
           <td width="196px" class="style8">
           </td>
           <td class="style6">
            <asp:Button ID="btnIFind" runat="server" Text="Find" ValidationGroup = "UserInf"/>  

当我只输入姓氏时,它不会从数据源中检索任何数据,但只有在我填写所有文本框时才有效。我怎样才能只需要一个文本框来检索信息?

查找按钮和文本框后面没有任何代码

4

1 回答 1

1

您正在寻找的是一个条件 where 子句。我创建的存储过程将在以下情况下返回数据:

  1. 仅提供姓氏
  2. 提供名字和姓氏
  3. 提供名字、姓氏和中间名缩写

如果您需要满足任何其他条件,只需修改存储过程。我创建存储过程的原因是因为查询现在比您开始使用的查询要大得多:

首先在 SQL 中创建这个存储过程:

CREATE PROCEDURE [dbo].[SearchUsers]
    @UserLastName VARCHAR(MAX),
    @UserFirstName VARCHAR(MAX) = 'NOVALUE',
    @UserMiddleInitial VARCHAR(MAX) = 'NOVALUE'
AS
BEGIN

SELECT [UserID], [UserSuffix], [UserAddressLine1], [UserAddressLine2], [UserCity], 
[UserState], [UserZipCode], [UserPhoneNumber], 
[UserEmail] FROM [AllUsers] 
WHERE 1 = (CASE 

WHEN @UserFirstName <> 'NOVALUE' AND
@UserMiddleInitial <> 'NOVALUE' AND
UserFirstName = @UserFirstName AND
UserLastName = @UserLastName AND
UserMiddleInitial = @UserMiddleInitial THEN 1

WHEN @UserFirstName <> 'NOVALUE' AND
@UserMiddleInitial = 'NOVALUE' AND
UserFirstName = @UserFirstName AND
UserLastName = @UserLastName THEN 1

WHEN @UserFirstName = 'NOVALUE' AND
@UserMiddleInitial = 'NOVALUE' AND
UserLastName = @UserLastName THEN 1
ELSE 0 END)

END

ASPX:

<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WildLife_EducationConnectionString %>"
    SelectCommand="SearchUsers" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="txtUILastName" Name="UserLastName" PropertyName="Text"
            Type="String" />
        <asp:ControlParameter ControlID="txtUIFirstName" DefaultValue="NOVALUE" Name="UserFirstName" PropertyName="Text"
            Type="String" />
        <asp:ControlParameter ControlID="txtUIMI" DefaultValue="NOVALUE" Name="UserMiddleInitial" PropertyName="Text"
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
<table id="USERINF">
    <tr>
        <td width="196px" class="style8">
            <asp:Label ID="lblFirstName" runat="server" Text="Please enter First Name:"></asp:Label>
        </td>
        <td class="style6">
            <asp:TextBox ID="txtUIFirstName" runat="server" MaxLength="20"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td width="196px" class="style8">
            <asp:Label ID="lblLastName" runat="server" Text="Please enter Last Name:"></asp:Label>
        </td>
        <td class="style6">
            <asp:TextBox ID="txtUILastName" runat="server" MaxLength="20"></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtUILastName"
                ValidationExpression="[a-zA-Z ]{1,20}" ForeColor="Red" ValidationGroup="UserInf"
                ErrorMessage="First Name must be only a-z,A-Z characters!" Display="Dynamic"></asp:RegularExpressionValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUILastName"
                ErrorMessage="Please enter Last Name!" ForeColor="Red" ValidationGroup="UserInf"
                Display="Dynamic"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td width="196px" class="style8">
            <asp:Label ID="lblMI" runat="server" Text="Please enter Middle Initials:"></asp:Label>
        </td>
        <td class="style6">
            <asp:TextBox ID="txtUIMI" runat="server" MaxLength="1" Width="30px" Style="text-align: center;
                margin-left: 2px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td width="196px" class="style8">
        </td>
        <td class="style6">
            <asp:Button ID="btnIFind" runat="server" Text="Find" ValidationGroup="UserInf" />
        </td>
    </tr>
</table>
<asp:ListView ID="listView" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="UserCityLabel" runat="server" Text='<%# Eval("UserCity") %>' />
            </td>
            <td>
                <asp:Label ID="UserEmailLabel" runat="server" Text='<%# Eval("UserEmail") %>' />
            </td>
        </tr>
    </ItemTemplate>
</asp:ListView>
</form>
于 2013-04-04T09:56:26.117 回答