Jump to content

jaylisto

Recommended Posts

been searching for the answer for a long time

 

i have this working code for a simple photo gallery for viewing photos

when the images are too many it will take a long time to load all the imagebuttons

 

i want to limit the imagebutton's to load only 10 photos, i will just put another next button for the next 10 photos and so on.

 

 

this foreach loop portion is where it loads all the pictures inside the folder named "loveones"

ImageButton imageButton = new ImageButton();FileInfo fileInfo = new FileInfo(strFileName);imageButton.ImageUrl = "~/pictures/loveones/" + fileInfo.Name;imageButton.Width = Unit.Pixel(200);imageButton.Height = Unit.Pixel(200);imageButton.Style.Add("padding", "5px");imageButton.Click += new ImageClickEventHandler(imageButton2_Click);Panel1.Controls.Add(imageButton);

i wish some one could help me here in w3 school. thanks.

 

Link to comment
Share on other sites

ok ill just put all the codes here.

using System;using System.Collections.Generic;using System.Linq; using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;namespace WebApplication5{public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){jayvalue();}}Response.Redirect("~/WebForm1.aspx");}private void jayvalue(){foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/pictures/loveones/")))                  // "this portion is the loop where it loads all the content of all images in the folder pictures/loveones/"{ImageButton imageButton = new ImageButton();FileInfo fileInfo = new FileInfo(strFileName);imageButton.ImageUrl = "~/pictures/loveones/" + fileInfo.Name;imageButton.Width = Unit.Pixel(200);imageButton.Height = Unit.Pixel(200);imageButton.Style.Add("padding", "5px");imageButton.Click += new ImageClickEventHandler(imageButton2_Click);Panel1.Controls.Add(imageButton);} }void imageButton_Click(object sender, ImageClickEventArgs e) { Response.Redirect(((ImageButton)sender).ImageUrl);}} }
Link to comment
Share on other sites

Ok, so rather than using the for-each to load everything, why not use the for-each to merely create an array of strings?

List<string> pixpaths = new List<string>(); // make this static/global/sessionint pixcounter = 0; // make this static/global/sessionint i = 0;foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/pictures/loveones/")))  {  pixpaths.Add(strFileName);  i++;}if (pixpaths.Count>10){  pixcounter += 10;}else{  pixcounter = 0;}for (i=0 ; ((i<pixpaths.Count)&&(i<10)) ; i++){ImageButton imageButton = new ImageButton();FileInfo fileInfo = new FileInfo(pixpaths[i]);imageButton.ImageUrl = "~/pictures/loveones/" + fileInfo.Name;imageButton.Width = Unit.Pixel(200);imageButton.Height = Unit.Pixel(200);imageButton.Style.Add("padding", "5px");imageButton.Click += new ImageClickEventHandler(imageButton2_Click);Panel1.Controls.Add(imageButton);} }void imageButton_Click(object sender, ImageClickEventArgs e) { Response.Redirect(((ImageButton)sender).ImageUrl);}} 

Then your next-button event routine would advance the photo display with a similar for-next loop. The global/static/session pixcounter would keep track of where you are in the array. I don't really remember how you create a global/static/session variable in ASP.NET but you can look that up.

Link to comment
Share on other sites

Ok, so rather than using the for-each to load everything, why not use the for-each to merely create an array of strings?

List<string> pixpaths = new List<string>(); // make this static/global/sessionint pixcounter = 0; // make this static/global/sessionint i = 0;foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/pictures/loveones/")))  {  pixpaths.Add(strFileName);  i++;}if (pixpaths.Count>10){  pixcounter += 10;}else{  pixcounter = 0;}for (i=0 ; ((i<pixpaths.Count)&&(i<10)) ; i++){ImageButton imageButton = new ImageButton();FileInfo fileInfo = new FileInfo(pixpaths[i]);imageButton.ImageUrl = "~/pictures/loveones/" + fileInfo.Name;imageButton.Width = Unit.Pixel(200);imageButton.Height = Unit.Pixel(200);imageButton.Style.Add("padding", "5px");imageButton.Click += new ImageClickEventHandler(imageButton2_Click);Panel1.Controls.Add(imageButton);} }void imageButton_Click(object sender, ImageClickEventArgs e) { Response.Redirect(((ImageButton)sender).ImageUrl);}} 

Then your next-button event routine would advance the photo display with a similar for-next loop. The global/static/session pixcounter would keep track of where you are in the array. I don't really remember how you create a global/static/session variable in ASP.NET but you can look that up.

 

i will definetly search for that global/static/session. by the way.

 

wow! you are amazing sir. i really upriciate your code. it really works. limitting 10 photos on the gallerynow the button "next" without redirecting the page will be my next problem.after pressing the button next , im planning to add numbers in the for loop portion like this one.

for (i=10 ; ((i<pixpaths.Count)&&(i<20)) ; i++)

 

is this possible?

 

i will post my code later after numerous attempt. thank you very much.

Link to comment
Share on other sites

Yeah, actually it would be best to pass the work over to Javascript after the initial load of 10 images, but I haven't written ASP.NET in awhile so I don't remember how to do that. The only trick would be passing the array of paths to Javascript.

Link to comment
Share on other sites

dude! i cant solve it. this portion will solve my problem on the link next button of my gallery. i dont know if i will start a new topic for this one.

 

can you please help me every time i press the button it should count the number of clicked.

i tried using static variable, yes it worked.

but every time i re run the code the static will continue to its last value.

 

 

public partial class WebForm1 : System.Web.UI.Page { static int count = 0; protected void Button1_Click(object sender, EventArgs e) { count += 1; Response.Write(count); } protected void Page_Load(object sender, EventArgs e) { } }

 

 

I tried putting count = 0; in the page load but it didnt work. it keeps the count into zero.

i think web form reruns the page load every time changes are done to the form.

 

how can i solve this scenario. :facepalm:

Link to comment
Share on other sites

yes it does work. thank you for your help.but when i implement it in the next button in my gallery issues keeps on coming.

 

i guest html destroys every variable i made every time i pressed "next button" that what makes it harder.

 

in the static variable, the next button needs to press twice before the next set of thumbnails.

 

i tried season state, static and viewstate

 

i feel so dumb right now :(

Link to comment
Share on other sites

here's what i did on viewstate.....

 

int j=0,k =12; protected void Page_Load(object sender, EventArgs e) { imagesthumbnailjaylisto(); if (!IsPostBack) { ViewState["clicks"] = j; //when the page loads the value of j is 0 by default and the value of k is 12 as declared on the top. ViewState["clicks2"] = k; } } protected void Button1_Click(object sender, EventArgs e) { if (ViewState["clicks"] != null && ViewState["clicks2"] != null) { j = (int)ViewState["clicks"] + 12; //this part really adds a value of 12 to each variable j and k k = (int)ViewState["clicks2"] + 12; } ViewState["clicks"] = j; ViewState["clicks2"] = k;

Response.Write(j); //this part is the proof that every time i pressed the next button it really keeps on adding value from viewstate["clicks"].

Response.Write(k);

}

 

 

on the part of your loop which is

 

for (i=j; ((i<pixpaths.Count)&&(i<k)) ; i++) // but the value of j and k keeps on default which is 0 and 12

Edited by jaylisto
Link to comment
Share on other sites

Javascript isn't Java. No, what I'm saying is that c# is the server-side code which can do secure operations and read the image-file names, but Javascript is the more efficient (client-side) way to merely update a screen, so to update those photos to 11-20 to 21-30 etc... would be more efficiently done with Javascript. The trick is that Javascript needs the array of filepaths and it needs to know where to put the photos on the screen. Also the "Next" button needs to call a Javascript function rather than a c# function.

 

http://msdn.microsoft.com/en-us/library/3hc29e2a%28v=vs.140%29.aspx

 

The simplest approach is probably to generate a Javascript array of the file paths as the page is rendered...

 

http://msdn.microsoft.com/en-us/library/ms178207%28v=vs.140%29.aspx

 

http://www.codeproject.com/Tips/91211/Pass-an-array-from-ASP-NET-server-to-javascript-cl

 

...and then assign the "Next" button to a Javascript handler function.

 

However... this may be getting too confusing. We could probably go back to the original code and create a very simple server-side solution where viewstate merely needs to remember the index number of the next photo to be loaded. Then simply regenerate the List of paths, start the for loop at the stored index value and display the next ten images. This would be inefficient -- but simple.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...