Asp.net 更改Gridview数据源或选择语句ijn代码?
我有一个带有sqldatasource的gridview,等等。对于网格,我还有 搜索文本框。如果用户需要过滤记录,我想 使用带有参数的sql过程动态调整SqlDataSource的SELECT语句。可以从文本框中获取值。我喜欢 所有自动分页、排序等功能,所以我不 我只想用老办法绑起来 有什么线索吗 谢谢Asp.net 更改Gridview数据源或选择语句ijn代码?,asp.net,.net,sql-server,parameters,sqldatasource,Asp.net,.net,Sql Server,Parameters,Sqldatasource,我有一个带有sqldatasource的gridview,等等。对于网格,我还有 搜索文本框。如果用户需要过滤记录,我想 使用带有参数的sql过程动态调整SqlDataSource的SELECT语句。可以从文本框中获取值。我喜欢 所有自动分页、排序等功能,所以我不 我只想用老办法绑起来 有什么线索吗 谢谢 <form id="form1" runat="server"> <div> <asp:SqlDataSource ID="SqlDa
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SelectCategorie" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="CategorieName"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products]">
</asp:SqlDataSource>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button3" runat="server" Text="Button" />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource3" DataTextField="ProductName"
DataValueField="ProductName">
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Change the datasource" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID"
DataFormatString="[Yassine {0}]" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
SortExpression="CategoryName" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
SortExpression="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
SortExpression="Discontinued" />
</Columns>
</asp:GridView>
<br />
<br />
</div>
</form>
这是我得到的错误:
过程或函数SelectCategorieParderDescription指定的参数太多我们对您要执行的操作使用类似的方法。困难的方法如下(你可以看到一篇关于这方面的文章:
ALTER PROCEDURE [dbo].[usp_SearchForObjectsForDashboard]
@searchTerm as varchar(250)
BEGIN
...
SET @sql = 'SELECT ...
...
WHERE someConditionOfYourOwn = whatever
AND ' + @searchTerm
EXEC(@sql)
END
希望它有帮助我将尽可能详细地阐述。为您的问题提供编码解决方案有点超出了范围。我将提供一些关于如何实现这一点的想法。希望其他人也能对此添加一些有价值的观点 搜索可以通过多种方式实现。这完全取决于您的要求和搜索的效率。从您的问题来看,我相信您正在尝试实现的是拥有一个搜索和多个过滤选项 比如说,说你想搜索员工(说你想按姓/姓/部门/经理搜索) 要开始简单,除了GridView和SqlDataSource之外,您还可以 1) 搜索框的文本框 2) 按选项搜索(4个单选按钮或带有
名字
,姓氏
,部门
,经理
)的下拉列表)
3) 钮扣
4) 具有两个参数(searchText
,searchBy
)的存储过程
在存储过程中,根据您的SearchBy
类型,您可以通过searchText
伪代码
if (searchBy == firstname)
{
set @sql = select ...... where firstName = searchText
}
else if (searchBy == lastname)
{
set @sql = select ...... where lastName = searchText
}
else if (searchBy == department)
{
set @sql = select ...... where department = searchText
}
else if (searchBy == manager)
{
set @sql = select ...... where manager = searchText
}
exec(@sql)
单击按钮,您将更改sqlDataSource选择参数
searchText
从文本框searchBy
从下拉列表或单选按钮
同样,这是最低限度的。您可以根据需要对此进行扩展。比如说
1) 在select查询中使用like
子句获得相似匹配,而不是精确匹配(在UI中,您可以为用户提供在精确、相似匹配之间进行选择的选项)
2) 您可以为用户提供在多个列中搜索的选项
诸如此类(选项无穷无尽;同样取决于您的需求)到目前为止您尝试了什么。你能分享一些代码吗?我相信在designer视图中,你已经用一个参数定义了SqlDataSource1。在Button2 click事件中,您正在更改select命令并添加参数。由于您的数据源相同,因此会保留上一个参数。因此你得到了太多的参数错误。是的,我得到了。。。任何解决问题的方法都是无效的。我已经把我的一些想法放在解决这个问题上了。检查我的答案;希望它能帮助您找到解决方案。
if (searchBy == firstname)
{
set @sql = select ...... where firstName = searchText
}
else if (searchBy == lastname)
{
set @sql = select ...... where lastName = searchText
}
else if (searchBy == department)
{
set @sql = select ...... where department = searchText
}
else if (searchBy == manager)
{
set @sql = select ...... where manager = searchText
}
exec(@sql)