Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. scroll through images

scroll through images

Scheduled Pinned Locked Moved ASP.NET
database
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toprogramminguy
    wrote on last edited by
    #1

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

    N 1 Reply Last reply
    0
    • T toprogramminguy

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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      How are you tracking the previous number?

      Navaneeth How to use google | Ask smart questions

      T 1 Reply Last reply
      0
      • N N a v a n e e t h

        How are you tracking the previous number?

        Navaneeth How to use google | Ask smart questions

        T Offline
        T Offline
        toprogramminguy
        wrote on last edited by
        #3

        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 the int 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
        
        N 1 Reply Last reply
        0
        • T toprogramminguy

          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 the int 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
          
          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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 the currentComics 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

          T 1 Reply Last reply
          0
          • N N a v a n e e t h

            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 the currentComics 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

            T Offline
            T Offline
            toprogramminguy
            wrote on last edited by
            #5

            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 the viewstate wrong?

            N 1 Reply Last reply
            0
            • T toprogramminguy

              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 the viewstate wrong?

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              toprogramminguy wrote:

              Am i using the viewstate wrong?

              page_load executes first before executing the button click handler. So first time ViewState["currentcomics"] will have NULL value. So do a NULL 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

              T 1 Reply Last reply
              0
              • N N a v a n e e t h

                toprogramminguy wrote:

                Am i using the viewstate wrong?

                page_load executes first before executing the button click handler. So first time ViewState["currentcomics"] will have NULL value. So do a NULL 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

                T Offline
                T Offline
                toprogramminguy
                wrote on last edited by
                #7

                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?

                N 1 Reply Last reply
                0
                • T toprogramminguy

                  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?

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  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

                  T 1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    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

                    T Offline
                    T Offline
                    toprogramminguy
                    wrote on last edited by
                    #9

                    Ok. Now the button is incrementing the currentcomic value but when the updatecontrol reloads nothing is displayed event though the updateprogress is showing so I know something is happening. Thanks for your help. I'll figure out the rest of it myself...hopefully:~ :-D

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups