Images not changing on new search. C# (asp.net project)
This uses bing's web service. I have a single image button and an array of image buttons. The single changes each time it is clicked to the next image in the array. The problem is if I click it and do a new search the array of image buttons does not change. CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bing_search.net.bing.api;
using System.Collections;
namespace bing_search
{
public partial class _Default : System.Web.UI.Page
{
static ArrayList images = new ArrayList();
static Image[] imagearry;
static ImageButton[] imgButtnsArray;
static int counter = 0;
int fooBarCount = 0;
int firstLoad = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DoItButton_Click(object sender, EventArgs e)
{
images.Clear();
imagearry = null;
imgButtnsArray = null;
BingService bs = new BingService();
net.bing.api.SearchRequest req = new SearchRequest();
req.AppId = "0B15AB60D625开发者_如何转开发A10059A4A04B68615C5B0D904CA9";
req.Query = SearchBox.Text;
req.Sources = new SourceType[] { SourceType.Image};
req.Market = "en-us";
req.Adult = AdultOption.Off;
req.Image = new ImageRequest();
req.Image.CountSpecified = true;
req.Image.Count = 50;
SearchResponse resp = bs.Search(req);
foreach (ImageResult result in resp.Image.Results)
{
Image im = new Image();
im.ImageUrl = result.MediaUrl;
im.Width = 200;
im.Height = 200;
images.Add(im);
//this.Controls.Add(im);
}
// Image lol = (Image)images[0];
int size = images.Count;
imagearry = new Image[size];
Type typ = typeof(Image);
imagearry = (Image [])images.ToArray(typ);
ImageButton1.ImageUrl = imagearry[0].ImageUrl;
int blaCount = 0;
ArrayList imgButtns = new ArrayList();
foreach (Image ii in images)
{
ImageButton imgb = new ImageButton();
imgb.Width = 200;
imgb.Height = 200;
imgButtns.Add(imgb);
}
size = imgButtns.Count;
imgButtnsArray = (ImageButton[])imgButtns.ToArray(typeof(ImageButton));
foreach (ImageButton iii in imgButtnsArray)
{
imgButtnsArray[fooBarCount].ImageUrl = imagearry[fooBarCount].ImageUrl;
Panel1.Controls.Add(iii);
fooBarCount++;
}
fooBarCount = 0;
counter = 0;
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
counter++;
heightLable.Text = "clicked";
Image tempImage = (Image)imagearry[counter];
ImageButton1.ImageUrl = tempImage.ImageUrl;
foreach (ImageButton iii in imgButtnsArray)
{
imgButtnsArray[fooBarCount].ImageUrl = imagearry[fooBarCount].ImageUrl;
Panel1.Controls.Add(iii);
fooBarCount++;
}
fooBarCount = 0;
counter = 0;
}
}
}
You reset both counters on every click, so its always starts from the same image.
fooBarCount = 0;
counter = 0;
also they are not static, so they reset to 0 anyway on every page load, and show the same image and not change.
If from the other hand the cache is the problem, because I can not know whats the image file name, and maybe this is the issue here, then try something like.
imgButtnsArray[fooBarCount].ImageUrl = imagearry[fooBarCount].ImageUrl + "?rnd=" + RandomNumber.ToString();
I changed ImageButton1_Click to this and now it works. thanks for the quick responses. Back to playing with .net
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
counter++;
heightLable.Text = "clicked";
Image tempImage = (Image)imagearry[counter];
ImageButton1.ImageUrl = tempImage.ImageUrl;
//Random RandomNumber = new Random(10000);
foreach (ImageButton iii in imgButtnsArray)
{
Panel1.Controls.Add((Image)imagearry[fooBarCount]);
fooBarCount++;
}
fooBarCount = 0;
counter = 0;
}
精彩评论