0

I have a web forms application that has only one web form. As part of a custom paging, I have hyperlinks for each index page under a grid view. These hyperlinks url location are to the same page with index number for the required page appended as query string.

string url = requestUrl;
int position = requestUrl.IndexOf('?');
if (position > 0)
{
         url = requestUrl.Substring(0, position);
}

string link = "<a  href='" + url + "?Index=[Index]&amp;Size=[Size]'><span class='page-numbers'>##Text##</span></a>";

Each time a hyperlink for a particular page is clicked, the data will be retrieved from database. Hence I need to pass the search parameters to the new index’s page also. I can pass it as a query string. But the challenge is in some cases the parameter content length can exceed query string limit.

What is the best approach to pass the search parameters to the new page when the hyperlinks are clicked?

Note: A simplified example of paging can be referred in custom-paging-in-asp-net-web-application

Note: The thumb of rule is not to use URLs longer than 2000 characters

EDIT

Based on the answer, I am using LinkButtons instead of Hypelinks. LinkButton can do a Postback. Also, I have separated the code in such a way that the pagination logic is in a User Control. It does not need any business specific data and search parameters. This code can be seen in https://codereview.stackexchange.com/questions/20510/custom-paging-in-asp-net-web-application

REFERENCES:

  1. What is the maximum length of a URL?
  2. What is the limit on QueryString / GET / URL parameters
  3. Custom paging with ASP.NET GridView
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

1 Answers1

1

Since you want to navigate to the same page and just pass a few parameters - instead of using a regular hyperlink, use a LinkButton which will process the request on the server side and refresh the data.

Whatever parameters you want to pass - you can place in hidden fields.

With this solution you have no need to use URLs and the data is POST -ed back to the server (instead of GET method which, as you mentioned, has its limits)

For example, in your aspx page:

<asp:LinkButton runat="server" ID="lbNextPage" onclick="lbNextPage_Click" />
<asp:HiddenField runat="server" ID="hdnData" />

Then in your code behind:

protected void lbNextPage_Click(object sender, EventArgs e)
{
        string data = hdnData.Value;

        // Refresh data based on data
        GridView1.DataSource = <NEW DATATABLE>;
        GridView1.DataBind();
}

Of course, you populate the hdnData with whatever parameters you wanted to pass to your hyperlink.

LCJ
  • 22,196
  • 67
  • 260
  • 418
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • I am not getting how LinkButton can resolve this issue. How can I get hidden field value of previous page? Can you please elaborate? – LCJ Jan 15 '13 at 10:38
  • 1
    It is NOT a previous page, it is *the same page*. You're performing a postback back to your same page - and passing whatever data you want in order to know how to update the grid (by requerying the server and performing a databind) - Added an example – Blachshma Jan 15 '13 at 10:40
  • Thanks for the POSTback approach. However, I don't understand why we need a HiddenField. Can't we directly take parameters from textbox values? – LCJ Jan 15 '13 at 17:01
  • @Lijo Since you didn't post any relevant code, I gave you the generic solution. You can use a textbox or any other control that its data is available on the server side... – Blachshma Jan 15 '13 at 18:36
  • Thanks. I have posted the code in http://codereview.stackexchange.com/questions/20510/custom-paging-in-asp-net-web-application. Do you have any comments on that? Note: I have separated the paging links into a user control. – LCJ Jan 21 '13 at 11:44