【问题标题】:SQL Search Query Using DropDown and Textbox in ASP.NET在 ASP.NET 中使用下拉和文本框的 SQL 搜索查询
【发布时间】:2014-11-26 05:53:53
【问题描述】:

大家好,我在实现基于 SQL 和 ASP.NET 的简单查询时遇到了一些麻烦。我已将我的数据网格与 Textbox 绑定,并且使用以下查询可以正常工作

SELECT Id, username, first_name, last_name, email, phone, picture, user_type, joining_date FROM admins WHERE (username= @un)

在这里,我只是将@un 与我的文本框绑定,它可以完美运行,但是一旦我将查询更改为:

SELECT Id, username, first_name, last_name, email, phone, picture, user_type, joining_date FROM admins WHERE (@opt= @un)

@opt 与我的 Dropdownlist 值属性绑定的位置,以便我可以搜索具有不同数据的不同列,例如在第一个查询中,我只搜索用户名,但在这里我也想搜索其他选项,所以我怎么能做到了吗?

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

     <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>

【问题讨论】:

  • 同时显示您的 .aspx 和 .cs 代码
  • 此标记代码中的下拉列表在哪里?
  • 哦抱歉没提,我已经提过了。

标签: c# sql asp.net visual-studio-2013


【解决方案1】:

你还没有在gridview中添加DataSourceID:

<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" >

【讨论】:

  • 添加后仍然存在我在查询生成器中测试过的相同问题,结果也没有显示在那里。
  • 删除 SQLDataSource 中的控制参数“search_term_box”
猜你喜欢
  • 2012-08-13
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多