Hello Everyone我在实现基于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>
答案 0 :(得分:0)
您尚未在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" >