LinqDataSource_Selecting不起作用

时间:2012-12-18 15:14:10

标签: asp.net linq

我使用了gridview和linqdatasourse,在点击搜索按钮后,gridview将由linq存储过程填充。

我是这样做的,但我的网格视图是空的。

似乎LinqDataSource2_Selecting不起作用。

请帮助解决问题是什么?

public void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
_DataContext = new EDMSDataContext();

 var subjectFilter = e.WhereParameters["Subject"];



var query = _DataContext.spQuickSearchDoc(subjectFilter);


e.Result = query;
}

protected void btnSearch_Click(object sender, EventArgs e)
{
    _DataContext = new EDMSDataContext();
    this.LinqDataSource2.WhereParameters["Subject"].DefaultValue =    this.txtSearchKeywords.Text;

    GridViewDocuments.Visible = false;
    GridViewDocuments_Search.Visible = true;
    this.GridViewDocuments_Search.DataBind();

}

存储过程:

 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 + '%'

的GridView:

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

1 个答案:

答案 0 :(得分:1)

您需要将DataSourceID添加到GridView,如:

<asp:GridView ID="GridViewDocuments_Search" runat="server" 
   . .  DataSourceID="LinqDataSource2">

由于未设置,因此GridView被绑定为NULL。