Each row has a "View Large" link which passes the image id to another page whereupon the image is displayed in a DetailsView.
That page has a "back to album" hyperlink.
All this works nicely, however, no matter what page the user was on in the GridView, having clicked through to the DetailsView and then back to the GridView, the user ends up on page 1 of the GridView.
How can I ensure the user comes back to the same page in the GridView that they were on when they clicked through to the DetailsView? Do I need to pass the gv.PageIndex value and send it back; can I just somehow tell the GridView to "stay where it was"?
Help?
Here's the code for the GridView (it pulls the image info from a SQL db:
No. By default such behaviour is not supported. However u can pass current page index via querystring to detailsview page and on click of "back to album" u can re-pass the same page index to the grid's page,, in page load of Grid page retrieve page index.
Then in grid binding function set current index to that 1.
I would tend not to use the Back Button is the browser (but if you really insist, there is a way - give this
article a read) but instead have a "Back to Photos" link or something this way you can control the pageindex of the GridView via the url querystring, see below:
Yes, I'm guessing this is the way to do it. But...
... if you look at the posted code, I have a Hyperlink within a TemplateField (which is currently passing the PictureID to the DetailsView). How do I ADD the GridView's current PageIndex value to THIS querystring? Could you give me the code, please?
And, yes, I'm guessing just passing it back in the QueryString of the "Back to Album" link is the way to get it back but...
... what do I do with it back in the GridView page? Could you supply the code to make the GridView pick up the returned value from the QueryString.
I'm not using the browser's back button and I am using a "Back to..." link.
Could I get your code in Visual Basic please and could you add code for handling the PageIndex when it is "returned" to the GridView page from the DetailsView page's "Back to..." link?
Sometimes requirements like these are handled with page properties that persist through the Session bag.
Here's Dave's code, converted. (will also need a placeholder to make up the querystring parameter, or do you already have it in there?). And, I've added a check for the RowType:
Protected Sub GridView1_RowDataBound(ByVal sender As [Object], ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Find your hyperlink.
Dim link As HyperLink = DirectCast(e.Row.FindControl("HyperLink1"), HyperLink)
link.NavigateUrl = link.NavigateUrl & "PageNo=" & GridView1.PageIndex.ToString()
End If
End Sub
** if you have other querystring parameters going in for the detailsview, which I presume you must have, you will have to do something like this:
on the page with the DetailsView, check for PageNo and update the PostBackUrl/NavigateUrl property of your LinkButton/HyperLink
Protected Sub page_load (.....
If Not string.IsNullOrEmpty(Request.QueryString("PageNo")) Then
YourBackLinkButton.PostBackUrl = "YourGridViewPage.aspx?PageNo=" & Request.QueryString("PageNo")
End If
End Sub
on the page with the GridView, same logic:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Dim index As Integer = 1
Dim temp As String
If Not String.IsNullOrEmpty(Request.QueryString("PageNo")) Then
temp = Request.QueryString("PageNo").ToString()
index = Convert.ToInt32(temp)
End If
Thank you for the link to the C#/VB converter; I've bookmarked that.
This is making some sense to me (a little over my head).
Not sure what you mean by: (will also need a placeholder to make up the querystring parameter, or do you already have it in there?). The QueryString parameter is simply what you see in the GridView HyperLinkField; I have nothing else.
Also, could you help a little further with the code behind for the link back, please? This link is, again, a HyperLinkField, this time within the DetailsView itself, rather than a separate link. It needs to be like this because the album in question belongs
to a particular User whose ID is passed to the DetailsView by the GridView and back again so that the user's album and no-one else's is displayed. Again, I'll need to ADD the PageIndex to an already existing QueryString.
Here's the code for the DetailsView (including link back, lines 11-15):
Not sure what you mean by: (will also need a placeholder to make up the querystring parameter, or do you already have it in there?). The QueryString parameter is simply what you see in the GridView HyperLinkField; I have nothing else.
nothing too complicated: if you were having a querystring string like "somepage.aspx?UserID=", you could just append PageNo directly to it, as it would turn out to be "somepage.aspx?UserID=PageNo=1" ...see??
back to the detailsview, you could handle its DataBound event (wire it up using the properties window) ...and in there find the HyperLinkField with something like
HyperLink hlink = dvLargeImage.Rows[0].Controls[0] as HyperLink; //convert to vb.net
if (hlink != null)
{
if (!string.IsNullOrEmpty(Request.QueryString["PageNo"]))
{
hlink.NavigateUrl = NagivateUrl + "&PageNo=" + Request.QueryString["PageNo"];
}
}
banksidepoet
Participant
774 Points
862 Posts
How to pass the PageIndex of a GridView from one page to another.
May 24, 2010 09:38 PM|LINK
Hi.
I have a GridView which contains photographs.
Each row has a "View Large" link which passes the image id to another page whereupon the image is displayed in a DetailsView.
That page has a "back to album" hyperlink.
All this works nicely, however, no matter what page the user was on in the GridView, having clicked through to the DetailsView and then back to the GridView, the user ends up on page 1 of the GridView.
How can I ensure the user comes back to the same page in the GridView that they were on when they clicked through to the DetailsView? Do I need to pass the gv.PageIndex value and send it back; can I just somehow tell the GridView to "stay where it was"?
Help?
Here's the code for the GridView (it pulls the image info from a SQL db:
<asp:GridView ID="gvDisplayImages" runat="server" EmptyDataText="There are currently no photographs in this category." AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PictureID" DataSourceID="picturesDataSource" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" RowStyle-BorderColor="#96FF32" RowStyle-BorderStyle="Dotted" RowStyle-BorderWidth="1px" CellPadding="5" PageSize="5" Width="800px"> <PagerSettings Mode="NumericFirstLast" /> <RowStyle BorderColor="#96FF32" BorderWidth="1px" BorderStyle="Dotted" HorizontalAlign="Center"></RowStyle> <EmptyDataRowStyle BorderColor="#96FF32" BorderStyle="Dotted" BorderWidth="1px" Font-Size="Medium" ForeColor="Red" /> <Columns> <asp:HyperLinkField DataNavigateUrlFields="PictureID" DataNavigateUrlFormatString="PhotoView.aspx?ID={0}" Text="View Large" > <ControlStyle ForeColor="#96FF32" /> </asp:HyperLinkField> <asp:TemplateField HeaderText="Filename" InsertVisible="False" SortExpression="PictureID"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("PictureID") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("PictureID") %>'></asp:Label> .jpg </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="UploadedOn" HeaderText="Date Added" SortExpression="UploadedOn" /> <asp:ImageField DataImageUrlField="PictureID" DataImageUrlFormatString="~/Photographs/{0}.jpg"> <ControlStyle Width="100px" /> </asp:ImageField> </Columns> <PagerStyle ForeColor="Black" BackColor="White" /> <SelectedRowStyle HorizontalAlign="Left" /> <HeaderStyle ForeColor="#96FF32" HorizontalAlign="Left" /> <EditRowStyle HorizontalAlign="Left" /> </asp:GridView>interrupt198...
Member
264 Points
57 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 08:19 AM|LINK
If you use a child window, using javascript "window.open(url , parameterlist)" , i guess its simple, on closing new
child window, you ld be on your last position in album. well you can give child window a look like popup using proper
parameters like
window.open("DetailImagePageUrl?id="+obj.id,"test","location=0,status=0,scrollbars=1,toolbar=0,menubar=0,addressbar=0,
width=800,height=675,left = 262,top = 84");
If my post solved your problem, Mark it as answer
niksv
Contributor
5925 Points
1115 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 08:46 AM|LINK
No. By default such behaviour is not supported. However u can pass current page index via querystring to detailsview page and on click of "back to album" u can re-pass the same page index to the grid's page,, in page load of Grid page retrieve page index. Then in grid binding function set current index to that 1.
Dave_Winches...
Contributor
3051 Points
716 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 08:56 AM|LINK
Hi
I would tend not to use the Back Button is the browser (but if you really insist, there is a way - give this article a read) but instead have a "Back to Photos" link or something this way you can control the pageindex of the GridView via the url querystring, see below:
Add PageIndex to GridView Item Link
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) { // Find your hyperlink. HyperLink link = (HyperLink)e.Row.FindControl("HyperLink1"); link.NavigateUrl = link.NavigateUrl + GridView1.PageIndex.ToString(); }CodeBehind for GridView
void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { int index = 1; string temp; if (!String.IsNullOrEmpty(Request.QueryString["page"])) { temp = Request.QueryString["page"].ToString(); index = Convert.ToInt32(temp); } GridView1.PageIndex = index; } }Sorry, not had a chance to test but just to give you the idea. Hope this helps.
Regards
David
Please mark as ANSWER if this is the solution.
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 12:54 PM|LINK
To interrupt1985
Thank you for your response but I want to avoid JavaScript at all costs.
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 12:58 PM|LINK
To nicksv
Thank you for your response.
Yes, I'm guessing this is the way to do it. But...
... if you look at the posted code, I have a Hyperlink within a TemplateField (which is currently passing the PictureID to the DetailsView). How do I ADD the GridView's current PageIndex value to THIS querystring? Could you give me the code, please?
And, yes, I'm guessing just passing it back in the QueryString of the "Back to Album" link is the way to get it back but...
... what do I do with it back in the GridView page? Could you supply the code to make the GridView pick up the returned value from the QueryString.
Thanks.
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 01:03 PM|LINK
To Dave Winchester,
I'm not using the browser's back button and I am using a "Back to..." link.
Could I get your code in Visual Basic please and could you add code for handling the PageIndex when it is "returned" to the GridView page from the DetailsView page's "Back to..." link?
Thanks.
PeteNet
All-Star
81342 Points
11398 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 01:37 PM|LINK
Use an online convertor: http://www.developerfusion.com/tools/convert/csharp-to-vb/ ...keep it handy.
Sometimes requirements like these are handled with page properties that persist through the Session bag.
Here's Dave's code, converted. (will also need a placeholder to make up the querystring parameter, or do you already have it in there?). And, I've added a check for the RowType:
Protected Sub GridView1_RowDataBound(ByVal sender As [Object], ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Find your hyperlink.
Dim link As HyperLink = DirectCast(e.Row.FindControl("HyperLink1"), HyperLink)
link.NavigateUrl = link.NavigateUrl & "PageNo=" & GridView1.PageIndex.ToString()
End If
End Sub
** if you have other querystring parameters going in for the detailsview, which I presume you must have, you will have to do something like this:
link.NavigateUrl = link.NavigateUrl & "&PageNo=" & GridView1.PageIndex.ToString()
on the page with the DetailsView, check for PageNo and update the PostBackUrl/NavigateUrl property of your LinkButton/HyperLink
Protected Sub page_load (.....
If Not string.IsNullOrEmpty(Request.QueryString("PageNo")) Then
YourBackLinkButton.PostBackUrl = "YourGridViewPage.aspx?PageNo=" & Request.QueryString("PageNo")
End If
End Sub
on the page with the GridView, same logic:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Dim index As Integer = 1
Dim temp As String
If Not String.IsNullOrEmpty(Request.QueryString("PageNo")) Then
temp = Request.QueryString("PageNo").ToString()
index = Convert.ToInt32(temp)
End If
GridView1.PageIndex = index
End If
End Sub
Peter
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 02:40 PM|LINK
To PeteNet,
Thank you for the link to the C#/VB converter; I've bookmarked that.
This is making some sense to me (a little over my head).
Not sure what you mean by: (will also need a placeholder to make up the querystring parameter, or do you already have it in there?). The QueryString parameter is simply what you see in the GridView HyperLinkField; I have nothing else.
Also, could you help a little further with the code behind for the link back, please? This link is, again, a HyperLinkField, this time within the DetailsView itself, rather than a separate link. It needs to be like this because the album in question belongs to a particular User whose ID is passed to the DetailsView by the GridView and back again so that the user's album and no-one else's is displayed. Again, I'll need to ADD the PageIndex to an already existing QueryString.
Here's the code for the DetailsView (including link back, lines 11-15):
<asp:DetailsView ID="dvLargeImage" runat="server" Height="50px" Width="450px" AutoGenerateRows="False" DataKeyNames="PictureID" DataSourceID="pictureDataSource" GridLines="None" AllowPaging="True" PagerStyle-Height="40px" PagerStyle-HorizontalAlign="Center" PagerStyle-VerticalAlign="Middle" CellSpacing="5" HorizontalAlign="Center"> <PagerSettings Mode="NextPrevious" NextPageText="" PreviousPageText="" NextPageImageUrl="~/Resources/Graphics/buttonNext.png" PreviousPageImageUrl="~/Resources/Graphics/buttonPrevious.png" /> <PagerStyle Font-Bold="False" ForeColor="White" Width="450px" /> <Fields> <asp:HyperLinkField DataNavigateUrlFields="UserId" DataNavigateUrlFormatString="AlbumView.aspx?ID={0}" Text="<< Back to album."> <ControlStyle ForeColor="White" /> </asp:HyperLinkField> <asp:ImageField DataImageUrlField="PictureID" DataImageUrlFormatString="~/Photographs/{0}.jpg"> <ControlStyle BorderColor="#96FF32" BorderStyle="Solid" BorderWidth="5px" Width="450px" /> </asp:ImageField> <asp:TemplateField> <InsertItemTemplate> <asp:Label ID="Label3" runat="server" Text="You are currently viewing image: "></asp:Label> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PictureID") %>'></asp:TextBox> <asp:Label ID="Label2" runat="server" Text=".jpg"></asp:Label> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text="You are currently viewing image: "></asp:Label> <asp:Label ID="Label1" runat="server" Text='<%# Bind("PictureID") %>'></asp:Label> </ItemTemplate> <ItemStyle Font-Bold="True" HorizontalAlign="Center" /> </asp:TemplateField> <asp:HyperLinkField DataNavigateUrlFields="PictureID,AdminID" DataNavigateUrlFormatString="PriceList.aspx?ID={0}&AdID={1}" Text="Buy This Photograph"> <ControlStyle ForeColor="#55DB32" BackColor="White" BorderColor="#96FF2B" BorderStyle="Double" BorderWidth="2px" Font-Bold="False" Height="40px" Width="160px" /> <ItemStyle HorizontalAlign="Center" BackColor="Black" BorderStyle="None" Font-Bold="True" ForeColor="#96FF32" Height="60px" VerticalAlign="Middle" Width="450px" /> </asp:HyperLinkField> </Fields> </asp:DetailsView>PeteNet
All-Star
81342 Points
11398 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 03:10 PM|LINK
nothing too complicated: if you were having a querystring string like "somepage.aspx?UserID=", you could just append PageNo directly to it, as it would turn out to be "somepage.aspx?UserID=PageNo=1" ...see??
back to the detailsview, you could handle its DataBound event (wire it up using the properties window) ...and in there find the HyperLinkField with something like
HyperLink hlink = dvLargeImage.Rows[0].Controls[0] as HyperLink; //convert to vb.net
if (hlink != null)
{
if (!string.IsNullOrEmpty(Request.QueryString["PageNo"]))
{
hlink.NavigateUrl = NagivateUrl + "&PageNo=" + Request.QueryString["PageNo"];
}
}
Peter