DatePager and altering the querystring


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:-[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:-


        <asp:ListView ID="lvPhotoViewer" runat="server" GroupItemCount="1"
             <table ID="groupPlaceholderContainer" runat="server" border="1">                               
                  <tr ID="groupPlaceholder" runat="server">
             <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") %>' />
              <tr ID="itemPlaceholderContainer" runat="server">
                   <td ID="itemPlaceholder" runat="server">
    <td align="center">
        <asp:DataPager ID="DataPager1" runat="server"
        PagedControlID="lvPhotoViewer" PageSize="1"
            <asp:NextPreviousPagerField ButtonType="Link"
            PreviousPageText="<< Previous" NextPageText="Next >>"/>


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;User ID=wbsd;Password=******");
        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());
        return (pageno - 1);
    public DataTable GetPhoto(string query)
        SqlConnection con = new SqlConnection("Data;User ID=wbsd;Password=*******");
        SqlDataAdapter ada = new SqlDataAdapter(query, con);
        DataTable dtEmp = new DataTable();
        return dtEmp;
    protected void DataPager1_PreRender(object sender, EventArgs e)
        lvPhotoViewer.DataSource = GetPhoto("Select * from hussaini_photo_album where album_id = " + ViewState["hfAlbumID"].ToString());
    protected void lvPhotoViewer_SelectedIndexChanged(object sender, EventArgs e)


So how can i fix this?


The next/previous image links will just have to load the URL with the new query string values.


ok thanks,

Can u show where i need to do this? Will it just be a simple server.transfer?



but where does this reponse.redirect need to go? from the code i have shown above is there a specific area i need to target for the querystring to be changed?

You need to do Response.Redirect(“view-photo.aspx?PhotoID=15&AlbumID=9&UID=10”);

But you must obviously just set the new url something like this:

string url = "view-photo.aspx?AlbumID=";
url += Request.QueryString["AlbumID"];
url += "&UID=" + Request.QueryString["AlbumID"];
url += "&PhotoID=" + getNewID();


sorry i think im being vague, i know how to do the response.redirect. but will i set this in page load? or do i need to apply in somewhere else?

On the next or previous buttons click events

I think the DataPager control only works in conjunction with DataSourceControls. At least I never got it to work with explicit “DataSource” assignment.

If I have understood correctly you need to do the following:

  • in your ListView (lvPhotoViewer) add the PagePropertiesChanging event
  • add the following code to this event:
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

thanks i will try this and let you know how i get on


Thanks SlitheryImp for the reply.

But thats doesnt seem to work.

Basically what i am trying to do is alter the querystring to match the ID of the image that is being shown.

So when i get to the page and i click on “Next”. It shows the next image in the album. However the querystring stays the same.

For e.g.

Lets say my initial querystring is this:-[B]PhotoID=12[/B]&AlbumID=8&UID=12

Note the bold, I am dealing with the PhotoID. Now when i click on “Next” the querystring should change:-[B]PhotoID=13[/B]&AlbumID=8&UID=12

See bold now, the PhotoID has changed. This is because the ID of the image is different. so why does the ID remain unchanged and how can i make this work?


This question goes to everyone. :rolleyes:

It stays the same coz you are staying on the same page, just loading new data. You need to redirect for the url to change.

If you cannot edit the click events of the next and previous buttons, then would you maybe be able to just use js to app to the url, so that ur C# will know its changed?

something like:

or set a hidden var to the new ID value and you grab that value with your C#?

Not sure if those methods would work for you, but it is an option if it does.

Hmm, i dont know myself…

lets see what other responses we get see if someone knows :wink:

make a GET request instead of POSTing back.

you can configure this on the pager

Where do i need to make this GET request?

The code i currently have is shown in #1

Any ideas?

“You can also change the way a page is submitted to the server by setting the [COLOR=#0033cc]QueryStringField[/COLOR] property.”

Thanks i will have a go and see where i get up to…

Will keep you updated :wink:


I cant see anything about changing the datapager to a GET method. I managed to do something with the querystring but that also didnt help.

I managed to change the querystring to this:-[B]String=1[/B]

See bolded part.

But this isnt helpful. This is my datapager:-

        <asp:DataPager ID="DataPager1" runat="server" 
        PagedControlID="lvPhotoViewer" PageSize="1" 
        onprerender="DataPager1_PreRender" QueryStringField="String">
            <asp:NextPreviousPagerField ButtonType="Link"
            PreviousPageText="<< Previous" NextPageText="Next >>"/>

I need to change the PhotoID as the ID of every image is different.

There must be a way to do this.

Hope someone can help.


Can anyone help with this?