ASP.NET Forms Parameters on Links
Ok, let me start this with I am more of an MVC person. I have a result set that I am trying to add paging. All I really want is a previous and next button on my page which are Link controls. I need these controls to post back to the same page, but have a query parm like page=4. How do I add these links? I see the PostBackUrl property on the link. Should I just use Request.Url and stuff it into PostBackUrl?开发者_Python百科 Then I have to do string searches on the parm... it gets nasty. Is that the only way? I have tried ViewState, please dont suggest that... it is unpredictable garbage as far as I am concerned.
The PostBackUrl property is for cross-page postbacks, you don't want to do that.
If you want to use a POST, you can use a LinkButton, handle the click event, and rebind your result set. Something like
public void BackButton_Click(object sender, EventArgs e)
{
// Get existing page from session, viewstate, etc
// RebindGrid
}
If you want to use a GET, you can use a HyperLink and set the NavigateUrl property to the correct Url. Something like
int page;
if( !int.TryParse(Request.QueryString["page"], out page) )
page = 1;
if( page > 1 )
BackUrl.NavigateUrl = Request.Path + "?page=" + (page-1).ToString();
else
NextUrl.NavigateUrl = Request.path + "?page=" + (page+1).ToString();
// Note: not syntax/bounds checked
Edit: Perhaps what you're looking for a Post-Redirect-Get pattern?
Response.Redirect(HttpContext.Current.Request.Path + query, true);
You're talking about ASP.NET Webforms or MVC?
If you're talking about Webforms I don't think that you'll have success on that. I believe that you'll need to control this move between your pagination with Session or other way or re-bind your data for each page.
What I ended up doing was putting hiddens on the page for the vars I needed to pass back, then I read those. This is things like page number, which allows me to calculate prev and next page. Sorry to submit my own answer but this was a little odd and I thought I would tell what I ended up doing.
View state for some reason was empty every time, so it was doing me no good. This is in a DNN app, and I didnt want to spend time figuring out why viewstate was messed up.
精彩评论