scroll through images
-
Ok so I have a database of images, a certain amount of which i want to display on a webpage at a time. The way it is working at the moment is each time the "next" button is clicked, it uses Response.Redirect() and supplies a query string to select the next 6 images to display them. This works ok but each time the whole page has to load so I would prefer if i could use an updatepanel. I have tried this and instead of supplying a query string the button click event increments the number of selection of images to be displayed. However, when the updatepanel updates the previous number is lost and does not increment because it is set back at 0. Is there any other way to do this without loading the whole page each time. Sorry if I've explained it badly... if you need more info I can post the code... thanks :)
-
Ok so I have a database of images, a certain amount of which i want to display on a webpage at a time. The way it is working at the moment is each time the "next" button is clicked, it uses Response.Redirect() and supplies a query string to select the next 6 images to display them. This works ok but each time the whole page has to load so I would prefer if i could use an updatepanel. I have tried this and instead of supplying a query string the button click event increments the number of selection of images to be displayed. However, when the updatepanel updates the previous number is lost and does not increment because it is set back at 0. Is there any other way to do this without loading the whole page each time. Sorry if I've explained it badly... if you need more info I can post the code... thanks :)
How are you tracking the previous number?
Navaneeth How to use google | Ask smart questions
-
How are you tracking the previous number?
Navaneeth How to use google | Ask smart questions
If I understand which number you are talking about (the one for the update panel?) I just had it as an
int
which was set on the page_load event. Then when the button was clicked it incremented the number by 6 and called the getImage function which gets the images form the database. But when the updatepanel is updated it resets theint
back to the original number so it just loops... but what i cant understand is that it increments once but thats it:confused: It's probably something dumb but I cant figure it out.:rolleyes: like this anyway:public partial class comicBrowser : System.Web.UI.UserControl
{
public int currentComics;
private Panel thumbnails = new Panel();
private string connectionString;
protected void Page_Load(object sender, EventArgs e)
{
currentComics = 6;
setConnectionString();
UpdatePanel1.ContentTemplateContainer.Controls.Add(thumbnails);
accessDatatables();
}private void setConnectionString()//gets the connection string from the .comfig file { Configuration Config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/WebSite"); ConnectionStringSettings connString; if (0 < Config.ConnectionStrings.ConnectionStrings.Count) { connString = Config.ConnectionStrings.ConnectionStrings\["data"\]; if (null != connString) connectionString = connString.ConnectionString; } } private void accessDatatables() { SqlConnection conn = new SqlConnection(connectionString); SqlDataReader readr = null; SqlCommand cmd = null; try { conn.Open(); cmd = new SqlCommand("SELECT comicUrl, comicId, comicRating, comicName FROM comicDatabase WHERE (comicId <= " + currentComics + ") AND (comicId > " + (currentComics - 6) + ")", conn); readr = cmd.ExecuteReader(); while (readr.Read()) { loadMenu(readr.GetString(0), readr.GetInt32(1), readr.GetInt32(2), readr.GetString(3)); } } catch(Exception ex) { this.Page.Title = ex.ToString(); } finally { if (conn != null) conn.Close(); if (readr != null) readr.Dispose(); } } private void loadMenu(string
-
If I understand which number you are talking about (the one for the update panel?) I just had it as an
int
which was set on the page_load event. Then when the button was clicked it incremented the number by 6 and called the getImage function which gets the images form the database. But when the updatepanel is updated it resets theint
back to the original number so it just loops... but what i cant understand is that it increments once but thats it:confused: It's probably something dumb but I cant figure it out.:rolleyes: like this anyway:public partial class comicBrowser : System.Web.UI.UserControl
{
public int currentComics;
private Panel thumbnails = new Panel();
private string connectionString;
protected void Page_Load(object sender, EventArgs e)
{
currentComics = 6;
setConnectionString();
UpdatePanel1.ContentTemplateContainer.Controls.Add(thumbnails);
accessDatatables();
}private void setConnectionString()//gets the connection string from the .comfig file { Configuration Config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/WebSite"); ConnectionStringSettings connString; if (0 < Config.ConnectionStrings.ConnectionStrings.Count) { connString = Config.ConnectionStrings.ConnectionStrings\["data"\]; if (null != connString) connectionString = connString.ConnectionString; } } private void accessDatatables() { SqlConnection conn = new SqlConnection(connectionString); SqlDataReader readr = null; SqlCommand cmd = null; try { conn.Open(); cmd = new SqlCommand("SELECT comicUrl, comicId, comicRating, comicName FROM comicDatabase WHERE (comicId <= " + currentComics + ") AND (comicId > " + (currentComics - 6) + ")", conn); readr = cmd.ExecuteReader(); while (readr.Read()) { loadMenu(readr.GetString(0), readr.GetInt32(1), readr.GetInt32(2), readr.GetString(3)); } } catch(Exception ex) { this.Page.Title = ex.ToString(); } finally { if (conn != null) conn.Close(); if (readr != null) readr.Dispose(); } } private void loadMenu(string
toprogramminguy wrote:
protected void Page_Load(object sender, EventArgs e) { currentComics = 6; setConnectionString(); UpdatePanel1.ContentTemplateContainer.Controls.Add(thumbnails); accessDatatables(); }
There is the problem. Update panel doesn't refresh the page, but it does a postback and your page load will execute all the time. So guard the page load code with
!IsPostBack
check. Also thecurrentComics
variable is a class level variable and it's value will be reset each time. So you need to persist the value may be using viewstate or other persisting mechanisms. :)Navaneeth How to use google | Ask smart questions
-
toprogramminguy wrote:
protected void Page_Load(object sender, EventArgs e) { currentComics = 6; setConnectionString(); UpdatePanel1.ContentTemplateContainer.Controls.Add(thumbnails); accessDatatables(); }
There is the problem. Update panel doesn't refresh the page, but it does a postback and your page load will execute all the time. So guard the page load code with
!IsPostBack
check. Also thecurrentComics
variable is a class level variable and it's value will be reset each time. So you need to persist the value may be using viewstate or other persisting mechanisms. :)Navaneeth How to use google | Ask smart questions
Ok now I've added this:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
currentComics = 6;
setConnectionString();
UpdatePanel1.ContentTemplateContainer.Controls.Add(thumbnails);
accessDatatables();
}
else currentComics = (int)ViewState["currentcomics"];
}and this:
protected void right_Click(object sender, ImageClickEventArgs e)
{
currentComics += 6;ViewState\["currentcomics"\] = currentComics; thumbnails.Controls.Clear();//clears previous images accessDatatables();//gets and displays the images updatejavascript(); }
but when it gets to here(in the first code block):
else currentComics = (int)ViewState["currentcomics"];
then it says:
Object reference not set to an instance of an object.
Am i using theviewstate
wrong? -
Ok now I've added this:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
currentComics = 6;
setConnectionString();
UpdatePanel1.ContentTemplateContainer.Controls.Add(thumbnails);
accessDatatables();
}
else currentComics = (int)ViewState["currentcomics"];
}and this:
protected void right_Click(object sender, ImageClickEventArgs e)
{
currentComics += 6;ViewState\["currentcomics"\] = currentComics; thumbnails.Controls.Clear();//clears previous images accessDatatables();//gets and displays the images updatejavascript(); }
but when it gets to here(in the first code block):
else currentComics = (int)ViewState["currentcomics"];
then it says:
Object reference not set to an instance of an object.
Am i using theviewstate
wrong?toprogramminguy wrote:
Am i using the viewstate wrong?
page_load
executes first before executing the button click handler. So first timeViewState["currentcomics"]
will haveNULL
value. So do aNULL
check before you access the value. Something like..else currentComics = ViewState["currentcomics"] == null ? 6 : (int)ViewState["currentcomics"];
:)Navaneeth How to use google | Ask smart questions
-
toprogramminguy wrote:
Am i using the viewstate wrong?
page_load
executes first before executing the button click handler. So first timeViewState["currentcomics"]
will haveNULL
value. So do aNULL
check before you access the value. Something like..else currentComics = ViewState["currentcomics"] == null ? 6 : (int)ViewState["currentcomics"];
:)Navaneeth How to use google | Ask smart questions
Ya i have done that now, but it wasnt on the first run that the exception was thrown because it wouldnt have been a
postback
, it was when I clicked the next button which has me all confused again :rolleyes: :) does the accessDatatables(); get called when i call it in the buttonclick event handler or when the updatepanel is reloaded? -
Ya i have done that now, but it wasnt on the first run that the exception was thrown because it wouldnt have been a
postback
, it was when I clicked the next button which has me all confused again :rolleyes: :) does the accessDatatables(); get called when i call it in the buttonclick event handler or when the updatepanel is reloaded?toprogramminguy wrote:
but it wasnt on the first run that the exception was thrown
Yes. It will be thrown when you clock the next button for the first time. Your else portion will get executed, but since ViewState is not created for the key you are accessing, it will return NULL.
toprogramminguy wrote:
does the accessDatatables(); get called when i call it in the buttonclick event handler or when the updatepanel is reloaded?
I am not getting what you mean here. It will be called when the next button is clicked.
Navaneeth How to use google | Ask smart questions
-
toprogramminguy wrote:
but it wasnt on the first run that the exception was thrown
Yes. It will be thrown when you clock the next button for the first time. Your else portion will get executed, but since ViewState is not created for the key you are accessing, it will return NULL.
toprogramminguy wrote:
does the accessDatatables(); get called when i call it in the buttonclick event handler or when the updatepanel is reloaded?
I am not getting what you mean here. It will be called when the next button is clicked.
Navaneeth How to use google | Ask smart questions
Ok. Now the button is incrementing the
currentcomic
value but when the updatecontrol reloads nothing is displayed event though theupdateprogress
is showing so I know something is happening. Thanks for your help. I'll figure out the rest of it myself...hopefully:~ :-D