如何重置ASP.NET ObjectdataSource参数的值

时间:2022-05-02 05:52:21

I have a gridview that is bound to an objectdatasource. There is paging and it works fine. Now there is also a searchbox on the page, and if someone clicks on the search button I want to reset the startRow parameter to 0, but it doesn't work: the current page is passed to the Select method of the datasource.

我有一个绑定到objectdatasource的gridview。有分页,它工作正常。现在页面上还有一个搜索框,如果有人点击搜索按钮,我想将startRow参数重置为0,但它不起作用:将当前页面传递给数据源的Select方法。

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

and in the codebehind I try to do:

在代码隐藏中我尝试做:

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

but the get method of the objectdatasource is called with whatever page user was at the moment that he clicked the searchbutton.

但是,当用户点击搜索按钮时,用户所在的任何页面都会调用objectdatasource的get方法。

1 个解决方案

#1


1  

You can reset the StartRowIndex parameter in the OnSelecting event of ObjectDataSource.

您可以在ObjectDataSource的OnSelecting事件中重置StartRowIndex参数。

Also, since you require that, when SearchButton is clicked, objectDataSource should start with row 0, so you need to identify which control caused the postback, and if that is your search button, reset the StartRowIndex parameter.

此外,由于您需要在单击SearchButton时,objectDataSource应从第0行开始,因此您需要确定哪个控件导致回发,如果这是您的搜索按钮,请重置StartRowIndex参数。

Below 3 steps should be performed:

应执行以下3个步骤:

1.) Identify if search button was clicked

1.)确定是否单击了搜索按钮

a.) I will recommend to see this blog as to how to get the control especially a button, that caused postback.

a。)我会建议看看这个博客如何获得控件,特别是一个按钮,导致回发。

Basically idea is to use a HiddenField and set this hiddenField value to our Search button control name, whenever search button is clicked.

基本上,想法是使用HiddenField并在单击搜索按钮时将此hiddenField值设置为我们的搜索按钮控件名称。

We then use a global variable named say controlName to set the value from hidden field in Page_Load event.

然后,我们使用名为say controlName的全局变量来设置Page_Load事件中隐藏字段的值。

2.) Handle the OnSelecting event of ObjectDataSource.

2.)处理ObjectDataSource的OnSelecting事件。

3.) define an OnClientClick event for your Search button. Why this event is used is because, when search button is clicked, we will set the HiddenField value to ID of our SearchButton.

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

Also, include the below script in <head> tag of your .aspx markup

另外,在.aspx标记的标记中包含以下脚本

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

Code behind::

代码背后::

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

That's all to resetting the start row value of ObjectDataSource, when your search button is clicked.

这就是在单击搜索按钮时重置ObjectDataSource的起始行值的全部内容。

#1


1  

You can reset the StartRowIndex parameter in the OnSelecting event of ObjectDataSource.

您可以在ObjectDataSource的OnSelecting事件中重置StartRowIndex参数。

Also, since you require that, when SearchButton is clicked, objectDataSource should start with row 0, so you need to identify which control caused the postback, and if that is your search button, reset the StartRowIndex parameter.

此外,由于您需要在单击SearchButton时,objectDataSource应从第0行开始,因此您需要确定哪个控件导致回发,如果这是您的搜索按钮,请重置StartRowIndex参数。

Below 3 steps should be performed:

应执行以下3个步骤:

1.) Identify if search button was clicked

1.)确定是否单击了搜索按钮

a.) I will recommend to see this blog as to how to get the control especially a button, that caused postback.

a。)我会建议看看这个博客如何获得控件,特别是一个按钮,导致回发。

Basically idea is to use a HiddenField and set this hiddenField value to our Search button control name, whenever search button is clicked.

基本上,想法是使用HiddenField并在单击搜索按钮时将此hiddenField值设置为我们的搜索按钮控件名称。

We then use a global variable named say controlName to set the value from hidden field in Page_Load event.

然后,我们使用名为say controlName的全局变量来设置Page_Load事件中隐藏字段的值。

2.) Handle the OnSelecting event of ObjectDataSource.

2.)处理ObjectDataSource的OnSelecting事件。

3.) define an OnClientClick event for your Search button. Why this event is used is because, when search button is clicked, we will set the HiddenField value to ID of our SearchButton.

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

Also, include the below script in <head> tag of your .aspx markup

另外,在.aspx标记的标记中包含以下脚本

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

Code behind::

代码背后::

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

That's all to resetting the start row value of ObjectDataSource, when your search button is clicked.

这就是在单击搜索按钮时重置ObjectDataSource的起始行值的全部内容。