开发者

DataPager throws error when querystring value is invalid

开发者 https://www.devze.com 2023-02-20 15:29 出处:网络
I am using a Datapager in my project with a ListView control. The Datapager is set to use a querystring value Page, so that a typical URL looks like:

I am using a Datapager in my project with a ListView control. The Datapager is set to use a querystring value Page, so that a typical URL looks like:

http://mysite.com/Something.aspx?Page=2

The problem is that the ListView throws an ugly error if the pageview is invalid (0开发者_如何学编程, for example). The error is:

Specified argument was out of the range of valid values.
Parameter name: startRowIndex

The error originates in the ListView SetPageProperties method.

What is the best way to address this? Can I override the SetPageProperties method in some way, to check the startRowIndex parameter?

Edit: here's a very simple page that replicates the error: http://pastebin.com/3Mixy6aT

Edit 2: A copy of a simple webapplication project demonstrating the error: http://www.mediafire.com/?8r222x5qim6cwrm


This behavior is new in v4.0. I cannot reproduce it using v3.5. It appears to be a known bug in ASP.NET 4.0, but it is hard to find clear references via Google.

Rather than override the SetPageProperties of ListView, I created an event handler for the OnInit event of DataPager, which checks the relevant parameter of the query string. From my testing, the only thing that causes an exception is when the page number parameter was zero or negative. In that case, I force the data pager to ignore the query string (by setting the QueryStringField to null). This in turn will cause the data pager to default to the first page.

Here's the code:

    protected void DataPager_Init(object sender, EventArgs e)
    {
        var dp = sender as DataPager;
        if (dp == null)
        {
            throw new Exception("This Event handler only works for DataPager web controls.");
        }

        string strPageNum = Request.QueryString[dp.QueryStringField];
        if (strPageNum == null) return;  // param not there, DataPager will use default

        int pageNum = -1;
        if (int.TryParse(strPageNum, out pageNum) && pageNum > 0)
        {
            return; // valid page number parameter, let DataPager handle it
        }


        dp.QueryStringField = null; // Force default
    }

Of course, you will need to add OnInit="DataPager_Init" to the markup for the <asp:DataPager> web control. Good luck.


As with others who have commented, I am unable to reproduce your error. If the Page parameter in the query string is invalid in any way (e.g. parameter missing, number out of range, non-numeric data), the DataPager ignores it and defaults to page 1.

Also, the sample code at http://pastebin.com/3Mixy6aT throws an exception, but for a different reason. Specifically, there is no LayoutTemplate with a server control that has id itemPlaceholder. I added a layout template, and the data pager works fine, regardless of the page number you give it. For example:

      <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
          <LayoutTemplate>
              <asp:Literal ID="itemPlaceholder" runat="server"></asp:Literal>
               <br />
          </LayoutTemplate>
          <ItemTemplate>
              <%#Eval("UserID") %><br />
          </ItemTemplate>
      </asp:ListView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
           ConnectionString="Data Source=..."
           SelectCommand="SELECT * FROM [Users]">
      </asp:SqlDataSource>

      Page:
      <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" 
          PageSize="5" QueryStringField="Page">
          <Fields>
              <asp:NumericPagerField />
          </Fields>
      </asp:DataPager> 

Note that the SetPageProperties method is a member of DataPager, not ListView. Interestingly, if you use this method to set an invalid row number (e.g. -1) in the code behind, you will get an "argument out of range error". Moreover, this exception is thrown even if you are missing the LayoutTemplate, so it will mask any problems you may have with your layout template.

My conclusion is that your problem has nothing to do with the Page parameter in your query string. I recommend that you make sure that you LayoutTemplate is correct, and that you are not calling SetPageProperites in the code behind. (If you really need to set the page properties for some other reason, you can post a new question to deal with that.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号