locked
Foreach to image url RRS feed

  • Question

  • User-1330916695 posted

    I have to get the Id from each of the images below and tie them to an image...having a little trouble.

    if (IsPostBack)
    {
    var qs = Request.QueryString["Id"];

    var images = from p in db.AdImages
    where p.AdId == qs
    select p;

    int count = images.Count();

    FOREACH (ID IN images)

    {

    int ?? = ??

    img.imageurl = image.aspx?Id= ???

    }

    <asp:Repeater ID="ImageRepeater" runat="server">
    <ItemTemplate>
    <asp:Image ID="Img" runat="server" />
    </ItemTemplate>
    </asp:Repeater>

    Sunday, January 22, 2017 8:59 AM

All replies

  • User753101303 posted

    Hi,

    Adn the exact problem or your intent is ? With a repeater you would use ImageReapeater.DataSource=images:ImageRepeater.DataBind(); and the template added to the repeater would bind to a property for each object.

    ASP.NET 4.5 even have a a model binding feature. See https://www.sitepoint.com/asp-net-4-5-strongly-typed-data-controls-model-binding/  (GridView but applies as well to the Repeater control).

    Sunday, January 22, 2017 3:27 PM
  • User-1330916695 posted

    I just need to ids from images into an array

    Monday, January 23, 2017 12:07 AM
  • User-707554951 posted

    Hi scstevelong,

    For your problem, I suggest you could use the following code:

     <asp:Repeater ID="ImageRepeater" runat="server">
                    <ItemTemplate>
                        <asp:Image ID="Img" runat="server"  ImageUrl='<%#String.Format("image.aspx?Id={0}",Eval("ID"))%>'/>
                    </ItemTemplate>
                </asp:Repeater>

    CodeBehind:

      Database1Entities1 context = new Database1Entities1();
            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!IsPostBack)
                {
                    var  qs = Convert.ToInt32( Request.QueryString["Id"]);
                    var images = from m in context.ImagesTables where m.ID == qs select m;
                    int count = images.Count();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID",typeof(int));
                    foreach (var image in images)
                    {
                      var  id = image.ID;
                        dt.Rows.Add(id);              
                    }
                    ImageRepeater.DataSource = dt;
                    ImageRepeater.DataBind();
    
                }
            }

    Best Regards

    Cathy

    Tuesday, January 24, 2017 7:19 AM