我使用了 gridview 和 linqdatasourse ,单击搜索按钮后,gridview 将由 linq 存储过程填充。
我像下面那样做,但我有错误““'System.Web.UI.WebControls.LinqDataSourceSelectEventArgs.WhereParameters'不能像方法一样使用”
var subjectFilter = e.WhereParameters("Subject");
网格视图:
<asp:GridView ID="GridViewDocuments_Search"
runat="server"
AutoGenerateColumns=False
Visible="False"
onrowcommand="GridViewDocuments_Search_RowCommand"
DataKeyNames="DocID" PageSize="100">
<Columns>
<asp:TemplateField HeaderText = "Details">
<ItemTemplate>
<asp:Button ID ="btn_Show"
Text="Details"
runat= "server"
CommandName= "Details"
CommandArgument=
'<%#Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DocumentNo"
HeaderText="DocumentNo"
SortExpression="DocumentNo" />
<asp:BoundField DataField="title"
HeaderText="Title"
SortExpression="title" />
<asp:BoundField DataField="Docid"
HeaderText="Docid"
Visible="false" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource2"
runat="server"
ContextTypeName="EDMSDataContext"
OnSelecting="LinqDataSource2_Selecting">
<WhereParameters>
<asp:ControlParameter Name="Subject"
ControlID="txtSearchKeywords"
PropertyName="Text"
Type="String" />
</WhereParameters>
</asp:LinqDataSource>
代码:
protected void btnSearch_Click(object sender, EventArgs e)
{
_DataContext = new EDMSDataContext();
this.LinqDataSource2.WhereParameters["Subject"].DefaultValue =
this.txtSearchKeywords.Text;
this.GridViewDocuments_Search.DataBind();
}
public void LinqDataSource2_Selecting(object sender,
LinqDataSourceSelectEventArgs e)
{
_DataContext = new EDMSDataContext();
var subjectFilter = e.WhereParameters("Subject");
var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);
e.Result = query;
}
存储过程:
ALTER PROCEDURE [dbo].[spQuickSearchDoc]
@Searchtext varchar(50)=null
AS
select DocId,DocumentNo,Title,Unit
from tblDocuments
where DocumentNo like '%'+@SearchText + '%'
or Title like '%'+@SearchText + '%'
or Unit like '%'+@SearchText + '%'