I think the problem lies with the fact that the FindControl is looking for the ID of a HyperLink but I have a HyperLinkField within a DetailsView which, actually doesn't have an ID at all. Or is that nonsense?
In any case, what I have now is that, on first load, the GridView always goes to Page 2 and, whatever page I'm on when I click "View Large", when I return, I return to Page 2.
I've included the full markup and VB for both pages to help:
GridViewPage Markup
<%@ Page Title="Album View" Language="vb" AutoEventWireup="false" MasterPageFile="~/Resources/Master Pages/Base.Master" CodeBehind="AlbumView.aspx.vb" Inherits="Thinking.AlbumView1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<div id="albumViewAlign">
<!-- Currently logged-on user -->
<asp:Label ID="UserIdValue" runat="server" Visible="False"></asp:Label>
<!-- Data Sources -->
<asp:SqlDataSource ID="categoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ThinkingConnectionString %>"
SelectCommand="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = @UserId) ORDER BY [Name]">
<SelectParameters>
<asp:ControlParameter ControlID="UserIdValue" Name="UserId" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="picturesDataSource" runat="server"
CancelSelectOnNullParameter="False"
ConnectionString="<%$ ConnectionStrings:ThinkingConnectionString %>" SelectCommand="SELECT PictureID, UploadedOn
FROM Photographs
WHERE UserId = @UserId AND (CategoryID = @CategoryID OR @CategoryID IS NULL)
ORDER BY UploadedOn DESC">
<SelectParameters>
<asp:ControlParameter ControlID="UserIdValue" Name="UserId"
PropertyName="Text" />
<asp:ControlParameter ControlID="categories" Name="CategoryID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<h2>
View your album</h2>
<p>
Please note: once your account has been created, you have 28 days in which to
purchase your images. After this time, your account will become inactive.</p>
<p>
Choose a category:
<asp:DropDownList ID="categories" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="categoriesDataSource" DataTextField="Name"
DataValueField="CategoryID" CssClass="controlTextBox">
<asp:ListItem Value="">-- All --</asp:ListItem>
</asp:DropDownList>
</p>
<br />
<p>
Your photographs...
<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">
<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>
</p>
</div>
</asp:Content>
GridView Page VB
Public Partial Class AlbumView1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString
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
gvDisplayImages.PageIndex = index
End If
End Sub
'This code kindly supplied by PeteNet on ASP.NET forums
Protected Sub gvDisplayImages_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("HyperLink"), HyperLink)
link.NavigateUrl = link.NavigateUrl & "&PageNo=" & gvDisplayImages.PageIndex.ToString()
End If
End Sub
End Class
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class PhotoView
Inherits System.Web.UI.Page
Dim PictureID As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString
If Not Page.IsPostBack Then
If Request.QueryString("ID") IsNot Nothing Then
PictureID = Request.QueryString("ID").ToString()
End If
Else : PictureID = String.Empty
End If
End Sub
Private Sub dvLargeImage_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvLargeImage.DataBound
If Not String.IsNullOrEmpty(PictureID) Then
Dim dv As DetailsView = DirectCast(sender, DetailsView)
If dv IsNot Nothing Then
Dim myDataRowView As DataRowView = TryCast(dv.DataItem, DataRowView)
For iRowIndex As Integer = 0 To myDataRowView.DataView.Count - 1
If myDataRowView.DataView(iRowIndex).Row(0).ToString() = Request.QueryString("ID") Then
'found record
dv.PageIndex = iRowIndex
Exit For
End If
Next
End If
End If
Dim hlink As HyperLink = TryCast(dvLargeImage.Rows(0).Controls(0), HyperLink)
If hlink IsNot Nothing Then
If Not String.IsNullOrEmpty(Request.QueryString("PageNo")) Then
hlink.NavigateUrl = (hlink.NavigateUrl & "&PageNo=") + Request.QueryString("PageNo")
End If
End If
End Sub
End Class
I know this is a lot of code. Apologies! If you can find the time to track down where your suggestions are not working in all of this, I'd be very grateful.
I think the problem lies with the fact that the FindControl is looking for the ID of a HyperLink but I have a HyperLinkField within a DetailsView which, actually doesn't have an ID at all. Or is that nonsense?
I see what you're saying but the given code for the DetailsView would work (as long as you have the right index for the row) BUT now you'd have to apply the same type of code to find the HyperLink on the GridView. FindControl won't work on BoundFields, FYI.....and
we didn't have your markup for the GridView so couldn't have given the right code either.
so, try this for the GridView in its RowDataBound event:
Dim hlink As HyperLink = TryCast(e.Row.Cells(0).Controls(0), HyperLink) 'confirm that
Cells(0) is the right column for the HyperLinkField, indexes are 0-based
2nd Issue, that of defaulting to second page: because it is taking the index = 1, you could change to:
IfNot Page.IsPostBack Then Dim index AsInteger = 1 Dim temp AsString IfNotString.IsNullOrEmpty(Request.QueryString("PageNo")) Then temp = Request.QueryString("PageNo").ToString() index = Convert.ToInt32(temp)
gvDisplayImages.PageIndex = index
EndIf EndIf
banksidepoet
I know this is a lot of code. Apologies! If you can find the time to track down where your suggestions are not working in all of this, I'd be very grateful.
yeah, time as always is at a premium ..but as long as we're getting somewhere I'm ready to help. You see its not just one issue we're dealing with.
Okay, the GridView is now defaulting to Page 1, not 2, which is good.
The QueryString is not picking up the PageIndex, though: I can't see the parameters for the PageIndex in the status bar when I hover over the link (I CAN see the parameters for the Picture and UserIds I've been passing around, but no additionak, information
for the PageIndex.)
When I click the "Back to Album(GridView)" link, I go back to page 1 regardless of what page I was on when I moved to the DetailsView.
I have confirmed that the "View Large" link in the GridView is the first column, so 0.
I've included the VB for the GridView page (the other code is unaltered:
GridView Page VB
Public Partial Class AlbumView1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString
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)
gvDisplayImages.PageIndex = index
End If
End If
End Sub
'This code kindly supplied by PeteNet on ASP.NET forums
Protected Sub gvDisplayImages_RowDataBound(ByVal sender As [Object], ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Find your hyperlink.
Dim hlink As HyperLink = TryCast(e.Row.Cells(0).Controls(0), HyperLink)
hlink.NavigateUrl = hlink.NavigateUrl & "&PageNo=" & gvDisplayImages.PageIndex.ToString()
End If
End Sub
End Class
The QueryString is not picking up the PageIndex, though: I can't see the parameters for the PageIndex in the status bar when I hover over the link (I CAN see the parameters for the Picture and UserIds I've been passing around, but no additionak, information
for the PageIndex.)
...still explainable: I would think that the RowDataBound is not even firing (is it?) ....if not, you're not debugging! :) which you should!
try changing to:
ProtectedSub gvDisplayImages_RowDataBound(ByVal sender As [Object], ByVal e As GridViewRowEventArgs)
Handles gvDisplayImages.RowDataBound
banksidepoet
When I click the "Back to Album(GridView)" link, I go back to page 1 regardless of what page I was on when I moved to the DetailsView.
We're mearly there. That solved the problem of there being no PageNo parameter in the querystring from GridView to Details View.
Sorry for not debugging: wrists slapped; I deserve it! Fact is I don't know how but... another thread.
One last hurdle: the DetailsView to GridView link does not contain the PageNo parameter and, whatever page I pass forwards, I'm always on page 1 when I come back.
One last hurdle: the DetailsView to GridView link does not contain the PageNo parameter and, whatever page I pass forwards, I'm always on page 1 when I come back.
In the Databound event you set the pageindex and exit, does the code for the HyperLink execute? keep a break-point there and see.
Else it must not be finding the HyperLink. you would have to debug this one.
Thank you very much for all that effort. I've learned quite a lot from your answers. I will be reading that article on debugging today: you're right, it is essential to at least locate a problem even if the solution is still a little advanced.
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 06:08 PM|LINK
This isn't working, Pete.
I think the problem lies with the fact that the FindControl is looking for the ID of a HyperLink but I have a HyperLinkField within a DetailsView which, actually doesn't have an ID at all. Or is that nonsense?
In any case, what I have now is that, on first load, the GridView always goes to Page 2 and, whatever page I'm on when I click "View Large", when I return, I return to Page 2.
I've included the full markup and VB for both pages to help:
GridViewPage Markup
<%@ Page Title="Album View" Language="vb" AutoEventWireup="false" MasterPageFile="~/Resources/Master Pages/Base.Master" CodeBehind="AlbumView.aspx.vb" Inherits="Thinking.AlbumView1" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"> <div id="albumViewAlign"> <!-- Currently logged-on user --> <asp:Label ID="UserIdValue" runat="server" Visible="False"></asp:Label> <!-- Data Sources --> <asp:SqlDataSource ID="categoriesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ThinkingConnectionString %>" SelectCommand="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = @UserId) ORDER BY [Name]"> <SelectParameters> <asp:ControlParameter ControlID="UserIdValue" Name="UserId" PropertyName="Text" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="picturesDataSource" runat="server" CancelSelectOnNullParameter="False" ConnectionString="<%$ ConnectionStrings:ThinkingConnectionString %>" SelectCommand="SELECT PictureID, UploadedOn FROM Photographs WHERE UserId = @UserId AND (CategoryID = @CategoryID OR @CategoryID IS NULL) ORDER BY UploadedOn DESC"> <SelectParameters> <asp:ControlParameter ControlID="UserIdValue" Name="UserId" PropertyName="Text" /> <asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource> <h2> View your album</h2> <p> Please note: once your account has been created, you have 28 days in which to purchase your images. After this time, your account will become inactive.</p> <p> Choose a category: <asp:DropDownList ID="categories" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="categoriesDataSource" DataTextField="Name" DataValueField="CategoryID" CssClass="controlTextBox"> <asp:ListItem Value="">-- All --</asp:ListItem> </asp:DropDownList> </p> <br /> <p> Your photographs... <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"> <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> </p> </div> </asp:Content>GridView Page VB
Public Partial Class AlbumView1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString 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 gvDisplayImages.PageIndex = index End If End Sub 'This code kindly supplied by PeteNet on ASP.NET forums Protected Sub gvDisplayImages_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("HyperLink"), HyperLink) link.NavigateUrl = link.NavigateUrl & "&PageNo=" & gvDisplayImages.PageIndex.ToString() End If End Sub End ClassDetailsView Page Markup
<%@ Page Title="Photo View Page" Language="vb" AutoEventWireup="true" MasterPageFile="~/Resources/Master Pages/Base.Master" CodeBehind="PhotoView.aspx.vb" Inherits="Thinking.PhotoView" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"> <div id="photoViewAlign"> <!-- Data Sources --> <asp:SqlDataSource ID="pictureDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ThinkingConnectionString %>" SelectCommand="SELECT [PictureID], [UserId], [AdminID] FROM [Photographs]WHERE ([UserId] = @UserId) ORDER BY [PictureID] DESC"> <SelectParameters> <asp:ControlParameter ControlID="UserIdValue" Name="UserId" PropertyName="Text" /> </SelectParameters> </asp:SqlDataSource> <!-- Page --> <!-- Currently logged-on user --> <asp:Label ID="UserIdValue" runat="server" Visible="False"></asp:Label> <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> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PictureID") %>'></asp:TextBox> </EditItemTemplate> <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> </div> </asp:Content>DetailsView Page VB
Imports System.Data Imports System.Data.SqlClient Partial Public Class PhotoView Inherits System.Web.UI.Page Dim PictureID As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString If Not Page.IsPostBack Then If Request.QueryString("ID") IsNot Nothing Then PictureID = Request.QueryString("ID").ToString() End If Else : PictureID = String.Empty End If End Sub Private Sub dvLargeImage_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvLargeImage.DataBound If Not String.IsNullOrEmpty(PictureID) Then Dim dv As DetailsView = DirectCast(sender, DetailsView) If dv IsNot Nothing Then Dim myDataRowView As DataRowView = TryCast(dv.DataItem, DataRowView) For iRowIndex As Integer = 0 To myDataRowView.DataView.Count - 1 If myDataRowView.DataView(iRowIndex).Row(0).ToString() = Request.QueryString("ID") Then 'found record dv.PageIndex = iRowIndex Exit For End If Next End If End If Dim hlink As HyperLink = TryCast(dvLargeImage.Rows(0).Controls(0), HyperLink) If hlink IsNot Nothing Then If Not String.IsNullOrEmpty(Request.QueryString("PageNo")) Then hlink.NavigateUrl = (hlink.NavigateUrl & "&PageNo=") + Request.QueryString("PageNo") End If End If End Sub End ClassI know this is a lot of code. Apologies! If you can find the time to track down where your suggestions are not working in all of this, I'd be very grateful.
Mike
PeteNet
All-Star
81342 Points
11398 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 06:35 PM|LINK
I see what you're saying but the given code for the DetailsView would work (as long as you have the right index for the row) BUT now you'd have to apply the same type of code to find the HyperLink on the GridView. FindControl won't work on BoundFields, FYI.....and we didn't have your markup for the GridView so couldn't have given the right code either.
so, try this for the GridView in its RowDataBound event:
Dim hlink As HyperLink = TryCast(e.Row.Cells(0).Controls(0), HyperLink) 'confirm that Cells(0) is the right column for the HyperLinkField, indexes are 0-based
2nd Issue, that of defaulting to second page: because it is taking the index = 1, you could change to:
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)
gvDisplayImages.PageIndex = index
End If
End If
yeah, time as always is at a premium ..but as long as we're getting somewhere I'm ready to help. You see its not just one issue we're dealing with.
Peter
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 06:55 PM|LINK
Thanks for your patience, Pete.
Okay, the GridView is now defaulting to Page 1, not 2, which is good.
The QueryString is not picking up the PageIndex, though: I can't see the parameters for the PageIndex in the status bar when I hover over the link (I CAN see the parameters for the Picture and UserIds I've been passing around, but no additionak, information for the PageIndex.)
When I click the "Back to Album(GridView)" link, I go back to page 1 regardless of what page I was on when I moved to the DetailsView.
I have confirmed that the "View Large" link in the GridView is the first column, so 0.
I've included the VB for the GridView page (the other code is unaltered:
GridView Page VB
Public Partial Class AlbumView1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString 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) gvDisplayImages.PageIndex = index End If End If End Sub 'This code kindly supplied by PeteNet on ASP.NET forums Protected Sub gvDisplayImages_RowDataBound(ByVal sender As [Object], ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then ' Find your hyperlink. Dim hlink As HyperLink = TryCast(e.Row.Cells(0).Controls(0), HyperLink) hlink.NavigateUrl = hlink.NavigateUrl & "&PageNo=" & gvDisplayImages.PageIndex.ToString() End If End Sub End ClassPeteNet
All-Star
81342 Points
11398 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 07:33 PM|LINK
...still explainable: I would think that the RowDataBound is not even firing (is it?) ....if not, you're not debugging! :) which you should!
try changing to:
Protected Sub gvDisplayImages_RowDataBound(ByVal sender As [Object], ByVal e As GridViewRowEventArgs) Handles gvDisplayImages.RowDataBound
the above might be the reason.
Peter
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 07:51 PM|LINK
We're mearly there. That solved the problem of there being no PageNo parameter in the querystring from GridView to Details View.
Sorry for not debugging: wrists slapped; I deserve it! Fact is I don't know how but... another thread.
One last hurdle: the DetailsView to GridView link does not contain the PageNo parameter and, whatever page I pass forwards, I'm always on page 1 when I come back.
Mike
PeteNet
All-Star
81342 Points
11398 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 08:25 PM|LINK
Actually, that is very important, even for this thread. go through this one: http://www.beansoftware.com/ASP.NET-Tutorials/debuging-Breakpoints.aspx
In the Databound event you set the pageindex and exit, does the code for the HyperLink execute? keep a break-point there and see.
Else it must not be finding the HyperLink. you would have to debug this one.
Peter
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 08:49 PM|LINK
Thanks for the link. Will follow that up tomorrow.
I've done my best, for now, with the breakpoint: set here... "If hlink is Not Nothing Then"
When I debug, the Autos window reports the value of hlink at this point as "Nothing".
The HyperLinkField is, however, Row(0) so not getting why it isn't finding it.
Mike
PeteNet
All-Star
81342 Points
11398 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 25, 2010 11:22 PM|LINK
Mike,
I could do a quick debug for you, and here's what will find your HyperLink:
Dim hlink As HyperLink = DirectCast(dvLargeImage.Rows(0).Controls(1).Controls(0), HyperLink)
Hope now you could get all the pieces to work.
Peter
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 26, 2010 01:32 AM|LINK
All in all, I think you can really try this:
1) In the Gridview, please make the Details link (the link which you click will guide to another page to see a larger image) into a common HTML
link, like <a>
2) Your link should be this:
<a href='<%#string.Format("LargerPicture.aspx?id={0}&pageindex={1}",Eval("ImageId"),Page.GridView1.PageIndex%>'>Details</a>
3) When you go to LargerPicture.aspx, in the Back button, please say this:
Response.Redirect("Main.aspx?pageIndex="+Request["pageindex"]);
4) In the Main.aspx, do this:
if(!IsPostBack)
{
GridView.DataSource = xxx;
GridView.DataBind();
if(string.IsNullOrEmpty(Request["pageindex"]))
{
GridView.PageIndex = Convert.ToInt32(Request["pageindex"]);
}
}
banksidepoet
Participant
774 Points
862 Posts
Re: How to pass the PageIndex of a GridView from one page to another.
May 26, 2010 06:29 AM|LINK
To Pete.
That's sorted it!
Thank you very much for all that effort. I've learned quite a lot from your answers. I will be reading that article on debugging today: you're right, it is essential to at least locate a problem even if the solution is still a little advanced.
Thanks again,
Mike