2013-09-28 30 views
1

我有一個綁定到objectdatasource的gridview。有分頁,它工作正常。現在頁面上還有一個搜索框,如果有人點擊搜索按鈕,我想將startRow參數重置爲0,但它不起作用:當前頁面被傳遞給數據源的Select方法。如何重置ASP.NET ObjectdataSource參數的值

 <asp:GridView runat="server" ID="gvCars" 
     AutoGenerateColumns="false" 
     DataKeyNames="Id"    
     AllowPaging="true" AllowSorting="true" 
     PageSize="2" DataSourceID="dataSource"   
     OnDataBound="GridView_DataBound" OnRowCommand="gvCars_RowCommand" OnRowDataBound="gvCars_RowDataBound">    

     <Columns> 
     ... 
     </Columns>    

    </asp:GridView> 
    <asp:ObjectDataSource ID="dataSource" EnablePaging="true" runat="server" 
     SelectCountMethod="GetCount" 
     MaximumRowsParameterName="PageSize" 
     StartRowIndexParameterName="StartRow" SortParameterName="SortExpression" 
     SelectMethod="Get" 
     TypeName="DataSource"> 

     <SelectParameters> 
      <asp:Parameter Name="startRow" /> 
      <asp:Parameter Name="pageSize" /> 
      <asp:Parameter Name="sortExpression" />     
      <asp:ControlParameter Name="searchTerm" ControlID="txtSearchTerm" PropertyName="Text" /> 
     </SelectParameters> 
    </asp:ObjectDataSource> 

,並在代碼隱藏我嘗試這樣做:

protected void btnSearch_Click(object sender, EventArgs e) 
    { 
     dataSource.SelectParameters["startRow"].DefaultValue = "0"; 
     gvCars.DataBind(); 
    } 

但ObjectDataSource控件的get方法被調用任何網頁的用戶是在他點擊搜索按鈕的那一刻。

回答

1

您可以重置ObjectDataSourceOnSelecting事件中的StartRowIndex參數。此外,由於您需要這樣做,因此當單擊SearchButton時,objectDataSource應從第0行開始,因此您需要確定哪個控件導致了回發,如果這是您的搜索按鈕,請重置StartRowIndex參數。

下面3個步驟應執行:

1)確定是否搜索按鈕被點擊

一)我會建議看this blog至於如何得到控制特別是一個按鈕,導致回發。

基本上的想法是使用HiddenField並將此hiddenField值設置爲我們的搜索按鈕控件名稱,無論何時搜索按鈕被點擊。

然後,我們使用名爲say controlName的全局變量來設置Page_Load事件中隱藏字段的值。

2.)處理OnSelecting事件ObjectDataSource

3.)爲您的搜索按鈕定義一個OnClientClick事件。爲什麼使用這個事件是因爲,當點擊搜索按鈕時,我們將HiddenField值設置爲我們的SearchButton的ID。

<asp:ObjectDataSource ID="dataSource" 
    OnSelecting="dataSource_Selecting" ... /> 
<asp:Button ID="btnSearch" runat="server" 
     OnClick="btnSearch_Click" 
     OnClientClick = "SetSource(this.id)"/> 
<asp:HiddenField ID="hidSourceID" runat="server" /> 

另外,包括下面的腳本在.aspx標記

<script type = "text/javascript"> 
    function SetSource(SourceID) { 
     var hidSourceID = 
     document.getElementById("<%=hidSourceID.ClientID%>"); 
     hidSourceID.value = SourceID; 
    } 
</script> 

代碼<head>標籤背後::

public partial class Default: System.Web.UI.Page 
{ 
    string controlName = string.Empty; 

    // Page Load event 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (Request.Form[hidSourceID.UniqueID] != null &&  
       Request.Form[hidSourceID.UniqueID] != string.Empty) 
       { 
        controlName = Request.Form[hidSourceID.UniqueID]; 
       } 
     } 

    // OnSelecting event of ObjectDataSource 
     protected void dataSource_Selecting(object sender, 
              ObjectDataSourceSelectingEventArgs e) 
    { 
     // here controlName is a variable set in Page_Load event 
     if (controlName != null) 
     { 
      // check if your search button was clicked 
      if (controlName.Equals("btnSearch")) 
      { 
       // reset the startRowIndex to zero 
       // note that e.Arguments will work 
       // e.InputParameters will not work 
       e.Arguments.StartRowIndex = 0;     
      } 
     } 
    } 
} 

這是所有到復位ObjectDataSource控件的起始行值,當您的搜索按鈕被點擊。

相關問題