Faron
Faron

Reputation: 47

Dynamically insert images into JavaScript Slideshow Asp.net C#

I am trying to use this JavaScript Slideshow but, it is not dynamic and I need it dynamic. Normally, I would just use an ASP Repeater; however the script calls for an Array of Images, like so:

    imagearray: [
        ['<%= ResolveUrl("~/Images/Slide/1.jpg") %>'],
        ['<%= ResolveUrl("~/Images/slide/2.jpg") %>']

I need mine to load from sql db, so I thought maybe:

    imagearray: [
        ['<%: getJson() %>']

Then in c# I was trying to do something like so:

    public string getJson()
    {
        string programId = Request.QueryString["ProgramId"];
        DataTable dt = CatalogAccess.GetProgramImages(programId);
    }

Here is where I am really getting stuck. I was thinking maybe I could return my FileNames in a data table then convert them to an array of objects?

    public string getJson()
    {
        string programId = Request.QueryString["ProgramId"];
        DataTable dt = CatalogAccess.GetProgramImages(programId);
        dt.Columns.Add("FileName", typeof(string));
        for (int i = 1; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr["FileName"] = "~/Images/Slide/" + i.ToString();
            dt.Rows.Add(dr);
        }

        ArrayList arrayList = new ArrayList();
        //create arraylsit from DataTable
        foreach (DataRow dr in dt.Rows)
        {
            arrayList.Add(dr["FileName"]);
        }
        return arrayList;
    }

Obviously, this is not working; I'm not even sure I'm approaching this the right way.

In addition, to being able to return the FileNames from the db, I need to be able to add the file path, in c#, to it as well (that is why I tried adding it to the datarow). And then have all of this string to my JavaScript array.

Any help, at all, would be greatly appreciated; and if I am WAY off track please don't hesitate to say so and send me in the right direction.

Upvotes: 2

Views: 574

Answers (1)

Royi Namir
Royi Namir

Reputation: 148634

Let's recap :

looking at :

Console.WriteLine (new JavaScriptSerializer().Serialize( (new ArrayList(){1,2,3,}) ));

Will yield :

[1,2,3] as a string

So change your code to return

return new JavaScriptSerializer().Serialize(arrayList);

instead of

 return arrayList;

And then change :

imagearray: [
    ['<%: getJson() %>']

to

imagearray: <%: getJson() %>;

Upvotes: 2

Related Questions