tags:

views:

99

answers:

2

I am trying to display random images

heres my code


private void Page_Load(object sender, EventArgs e)
{
   int num1=0;
   Random randNum = new Random();
   num1 = randNum.Next(0, 9);
   Image1.ImageUrl = DisplayNumber(num1);
   Image1.Visible=true;
}

protected  string DisplayNumber(int i)
{
        string imagepath="";
        switch (i)
        {
            case 0:
                imagepath = "~/fordoctor/doctor_login/images/0.GIF";
                break;

            case 1:
                imagepath = "~/fordoctor/doctor_login/images/1.GIF";
                break;

            case 2:
                imagepath = "~/fordoctor/doctor_login/images/2.GIF";
                break;

            case 3:
                imagepath = "~/fordoctor/doctor_login/images/3.GIF";
                break;

            case 4:
                imagepath = "~/fordoctor/doctor_login/images/4.GIF";
                break;

            case 5:
                imagepath = "~/fordoctor/doctor_login/images/5.GIF";
                break;

            case 6:
                imagepath = "~/fordoctor/doctor_login/images/6.GIF";
                break;

            case 7:
                imagepath = "~/fordoctor/doctor_login/images/7.GIF";
                break;

            case 8:
                imagepath = "~/fordoctor/doctor_login/images/8.GIF";
                break;

            case 9:
                imagepath = "~/fordoctor/doctor_login/images/9.GIF";
                break;
        }
        Session["num1"] = imagepath;
        return imagepath;

    }

but it displays nothing i have even checked the images using Response.Write(Session["num1"].ToString()); and the images get displayed at the next page

A: 

Check the case of your GIF, should be lowercase (usually).

Antony Carthy
no its not a case sensitive problemthanks for your reply
Searock
+1  A: 

I suggest making your code similar to this:

private void Page_Load( object sender, EventArgs e ) {
    string imgUrl = GetRandomImageUrl();
    Session["num1"] = imgUrl;
    Image1.ImageUrl = imgUrl;
    Image1.Visible = true;
}

protected string GetRandomImageUrl() {
    Random r = new Random();
    return String.Format( "~/fordoctor/doctor_login/images/{0}.gif", r.Next( 0, 9 ) );
}
kitchen
thanks for making the code short
Searock