Hi,
I was wondering if this could be done. I have implemented a photo album which simply shows various pictures. Now i also have a DataReader acting as next/previous links.
When i first get to the photo album images i am represented with a querystring like so:-
http://csesalford.com/hussainwd9/view-photo.aspx?[B]PhotoID=15[/B]&AlbumID=9&UID=10
See in bold the PhotoID is what i will be referring to. So when i use the DataPager links to go to the next image, the querystring stays the same even though the PhotoID has changed. This does not the code from working and displaying the right image BUT i need the PhotID to be correct as i will have users comments on certain images.
So how can i make this work so that when i click on the next/previous link the querystring matches the PhotoID of the displayed image.
This is my code:-
.aspx
<table>
<tr>
<td>
<asp:ListView ID="lvPhotoViewer" runat="server" GroupItemCount="1"
onselectedindexchanged="lvPhotoViewer_SelectedIndexChanged">
<LayoutTemplate>
<table ID="groupPlaceholderContainer" runat="server" border="1">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td id="Td4" align="center" style="background-color: #eeeeee;">
<asp:Image runat="server" ID="imPhoto" Height="450px" Width="450px" ImageUrl='<%# string.Format("~/photos/{0}", Eval("photo")) %>' />
<br />
<asp:Label ID="DefaultPhotIDLabel" runat="server" Text='<%# Eval("photo_name") %>' />
</td>
</ItemTemplate>
<GroupTemplate>
<tr ID="itemPlaceholderContainer" runat="server">
<td ID="itemPlaceholder" runat="server">
</td>
</tr>
</GroupTemplate>
</asp:ListView>
</td>
</tr>
<tr>
<td align="center">
<asp:DataPager ID="DataPager1" runat="server"
PagedControlID="lvPhotoViewer" PageSize="1"
onprerender="DataPager1_PreRender">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link"
PreviousPageText="<< Previous" NextPageText="Next >>"/>
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string photoID = Request.QueryString["PhotoID"];
string albumID = Request.QueryString["AlbumID"];
ViewState["hfAlbumID"] = albumID;
//Get Page number by passing photo id
int index = GetPageNumber(int.Parse(photoID), int.Parse(albumID));
DataPager1.SetPageProperties(index, 1, true);
}
}
/// <summary>
/// Since the pagesize is 1 the row number can be taken as page number
/// </summary>
/// <param name="PhotoID"></param>
/// <param name="AlbumID"></param>
/// <returns></returns>
public int GetPageNumber(int PhotoID, int AlbumID)
{
SqlConnection con = new SqlConnection("Data Source=SQLB23.webcontrolcenter.com;User ID=wbsd;Password=******");
con.Open();
SqlCommand com = new SqlCommand("SELECT PageNumber FROM (SELECT row_number() Over (order by photo_id asc) AS PageNumber,photo_id,album_id FROM hussaini_photo_album where album_id=" + AlbumID.ToString() + ") As photos where photo_id=" + PhotoID.ToString() + " and album_id=" + AlbumID.ToString(), con);
SqlDataReader dr = com.ExecuteReader();
int pageno = 1;
if (dr.Read())
{
pageno = int.Parse(dr["PageNumber"].ToString());
}
dr.Close();
con.Close();
return (pageno - 1);
}
public DataTable GetPhoto(string query)
{
SqlConnection con = new SqlConnection("Data Source=SQLB23.webcontrolcenter.com;User ID=wbsd;Password=*******");
SqlDataAdapter ada = new SqlDataAdapter(query, con);
DataTable dtEmp = new DataTable();
ada.Fill(dtEmp);
return dtEmp;
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
lvPhotoViewer.DataSource = GetPhoto("Select * from hussaini_photo_album where album_id = " + ViewState["hfAlbumID"].ToString());
lvPhotoViewer.DataBind();
}
protected void lvPhotoViewer_SelectedIndexChanged(object sender, EventArgs e)
{
}
So how can i fix this?
Regards
Billy