0 Replies - 1324 Views - Last Post: 08 August 2013 - 08:26 AM Rate Topic: -----

#1 AOM_Set  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 151
  • Joined: 18-December 10

Get reference from database and show it in a src attribute

Posted 08 August 2013 - 08:26 AM

I have some references in a Mysql database, and I want to add these to a src attribute.
The html code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="slideshow">  
               <img id="ContentPlaceHolder1_imageView" src="" alt="" class="active" runat="server" />          
                           
      </div> 
</asp:Content>



The C# code to get the images from the database:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using App_Code;

public partial class WebLogic : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    
    }

    public string[] LoadImagesFromDB()
    {
        Sql sql = new Sql();
        string query = "SELECT Location FROM GalleryImages";
        MySqlCommand cmd = sql.Command(query);
        DataTable dt = sql.DataTable(cmd);

        string[] billeder = new string[dt.Rows.Count];

        for (int i = 0; i < billeder.Length; ++i )
        {
            images[i] += HttpUtility.UrlDecode(dt.Rows[i]["Location"].ToString());
        }        

        return images;
    }
}


The class that calls the above method, which also is the class that should pass the image references to the .aspx page where the src attribute is:
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index : System.Web.UI.Page
{
    private WebLogic logic = new WebLogic();

    protected void Page_Load(object sender, EventArgs e)
    {
        GetImageForView();
    }

    public void GetImageForView()
    {
        string[] images = logic.LoadImagesFromDB();
        ArrayList list = new ArrayList();
        
        for (int i = 0; i < images.Length; i++)
        {           
            list.Add(images[i].ToString());           
        }        

        ContentPlaceHolder1_imageView.Src = //What should I put here?
    }
}



I am pretty stuck, I have tried many things that I lost the idea. I appreciate all the help you can give. Also, I want to write good code from the begining, so that it saves me the headache later when I have to add to the code, or edit. So if you have anything to add/correct or improvements to what I have written so far, please feel free to critisize me.

So to summarize. How do I get the image references to the src attribute?

If I do:
ContentPlaceHolder1_imageView.Src = images[i].ToString();


It will show me the last image of the array. But I want it to show all images in the database (using a slideshow that I created in JQuery). Thanks

Ps. I also tried writing some ajax code, bu gave up on that. But here it is anyway:

$(document).ready(function () {
    getImages();
});

function getImages() {

    $.ajax({
        url: "Dummy.aspx/GetImages",
        type: "POST",
        data: "{}",
        contentType: "application/json; charset=utf8",
        dataType: "json",
        async: true,
        cache: false,
        succes: //What should be here?   
    });
    return false;
}


The GetImages seen at the url, is a method that was to return the image references.

This post has been edited by AOM_Set: 08 August 2013 - 08:42 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1