C# ASP.NET中使用下拉框和文本框的SQL搜索查询

C# ASP.NET中使用下拉框和文本框的SQL搜索查询,c#,sql,asp.net,visual-studio-2013,C#,Sql,Asp.net,Visual Studio 2013,大家好,我在实现基于SQL和ASP.NET的简单查询时遇到了一些麻烦。我已经用Textbox绑定了我的datagrid,使用下面的查询它可以正常工作 SELECT Id, username, first_name, last_name, email, phone, picture, user_type, joining_date FROM admins WHERE (username= @un) 在这里,我只是将@un与我的文本框绑定在一起,它工作得非常完美,但一旦我将查询更改为: 从管理员那

大家好,我在实现基于SQL和ASP.NET的简单查询时遇到了一些麻烦。我已经用Textbox绑定了我的datagrid,使用下面的查询它可以正常工作

SELECT Id, username, first_name, last_name, email, phone, picture, user_type, joining_date FROM admins WHERE (username= @un)
在这里,我只是将@un与我的文本框绑定在一起,它工作得非常完美,但一旦我将查询更改为:

从管理员那里(@opt=@un)选择Id、用户名、名字、姓氏、电子邮件、电话、图片、用户类型、加入日期

其中@opt与我的Dropdownlist value属性绑定,这样我就可以用不同的数据搜索不同的列。例如,在第一次查询中,我只搜索用户名,但在这里我还想搜索其他选项,那么我如何才能做到这一点

这是我的datagrid和sqldatasource的aspx,它没有代码隐藏文件,因为我已经通过VisualStudio绑定了属性

     <asp:DropDownList CssClass="form-control" Height="38px" Width="238px" ID="search_opt_box" runat="server" AutoPostBack="True">
 <asp:ListItem>All</asp:ListItem>
 <asp:ListItem Value="Id">ID</asp:ListItem>
 <asp:ListItem Value="username">By Username</asp:ListItem>
 <asp:ListItem Value="first_name">By First Name</asp:ListItem>
 <asp:ListItem Value="last_name">Last Name</asp:ListItem>
 <asp:ListItem Value="phone">By Phone</asp:ListItem>
 <asp:ListItem Value="email">By Email</asp:ListItem>
 <asp:ListItem Value="city">By City</asp:ListItem>
 <asp:ListItem Value="province">By Province</asp:ListItem>
 <asp:ListItem Value="country">By Country</asp:ListItem>
 <asp:ListItem Value="address">By Address</asp:ListItem>
 </asp:DropDownList>    
    <asp:GridView ID="search_by_name" runat="server" Width="1060px" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CssClass="table table-bordered table-striped table-hover tc-table table-primary footable" RowHeaderColumn="Id" DataKeyNames="Id">
         <Columns>
         <asp:TemplateField HeaderText="Picture" ControlStyle-Width="100px" ControlStyle-Height="100px">
         <ItemTemplate>
         <asp:Image ID="user_pic" runat="server" ImageUrl='<%# "fetch_user_img.ashx?username="+Eval("username") %>' />
         </ItemTemplate>
         </asp:TemplateField>
         <asp:BoundField DataField="Id" HeaderText="ID#" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
         <asp:BoundField DataField="username" HeaderText="Username" SortExpression="username" />
         <asp:BoundField DataField="first_name" HeaderText="First Name" SortExpression="first_name" />
         <asp:BoundField DataField="last_name" HeaderText="Last Name" SortExpression="last_name" />                     
         <asp:BoundField DataField="user_type" HeaderText="Category" SortExpression="user_type" />
         <asp:BoundField DataField="joining_date" HeaderText="Joining Date" SortExpression="joining_date" />
         <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
         <asp:BoundField DataField="phone" HeaderText="Phone#" SortExpression="phone" />
         </Columns>
         </asp:GridView>



         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnectionString %>" SelectCommand="SELECT Id, username, first_name, last_name, email, phone, picture, user_type, joining_date FROM admins WHERE (@opt= @un) ">
         <SelectParameters>
         <asp:ControlParameter ControlID="search_opt_box" Name="opt" PropertyName="SelectedValue" />
          <asp:ControlParameter ControlID="search_term_box" Name="un" PropertyName="Text" />
                                </SelectParameters>
                            </asp:SqlDataSource>

全部的
身份证件
按用户名
直呼其名
姓
通过电话
通过电子邮件
按城市
按省
按国家
按地址

您尚未在gridview中添加数据源ID:

<asp:GridView ID="search_by_name" runat="server" Width="1060px" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CssClass="table table-bordered table-striped table-hover tc-table table-primary footable" RowHeaderColumn="Id" DataKeyNames="Id" DataSourceId="SqlDataSource1" >


同时显示您的.aspx和.cs代码在此标记代码中的下拉列表在哪里?哦,很抱歉没有提到这一点,我现在已经提到了。添加了这一点之后,仍然存在我在查询生成器中测试过的同一问题,并且结果也没有显示在那里。请删除SQLDataSource中的控制参数“search\u term\u box”