• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

binding image control to dataset

I have a image control in my asp.net page. I am retrieving image paths and stored on dataset and want to bind to image control. (how will i do it without gridview)

example : if 10 image paths coming from dataset then it will automatically  display 10 images in webpage. How will i do it.
0
Tapan Pattanaik
Asked:
Tapan Pattanaik
  • 2
  • 2
2 Solutions
 
Kishan ZunjareCommented:
Hi TAPAN,

First you have to do for loop, get one by one path from dataset; take some placeholder in aspx page like any table or panel and then in For loop create new object of image and add this image control to that placeholder.


This is psuedo code;
for (int i = 0; i < datasetCount; i++){
Image image = new Image ();
Image1.ImageUrl = path;
placeholder.Controls.Add(image);
}

Hope this helps;
-Kishan
0
 
Tapan PattanaikSenior EngineerAuthor Commented:
Hi Kishan,

can you share more in depths.
0
 
Kishan ZunjareCommented:
//Convert DataSet to DataTable by getting DataTable at first zero-based index
DataTable myDataTable = myDataSet.Tables[0];  
//Loop over the rows.
foreach (DataRow row in myDataTable.Rows)
{
 Image image = new Image();
 Image1.ImageUrl =dRow[ "path"].ToString();
 placeholder.Controls.Add(image);
}

Hope this will help for you..
0
 
Robert SchuttSoftware EngineerCommented:
The previous solution should work fine, however it's also possible to use a Repeater and data binding, which is closer to how I read the question.

Put this in your aspx (change field names in Eval(...) to your own):
    <asp:Repeater ID="ImageRepeater1" runat="server">
        <ItemTemplate><div class="PrettyPicture"><img alt="<%# Eval("ImageDescription") %>" title="<%# Eval("ImageDescription") %>" src="<%# Eval("ImagePath") %>"></div></ItemTemplate>
    </asp:Repeater>

Open in new window

and put this style for example in your <head>:
    <style type="text/css">
        div.PrettyPicture 
        {
            border: solid 2px gray;
            background-color: silver;
            margin: 4px;
            padding: 6px;
            float: left;
        }
    </style>

Open in new window

and in code-behind, for testing I just used Page load, the test data you can leave out of course, just bind to your own dataset/table:
        protected void Page_Load(object sender, EventArgs e) {
            // test data
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("ImageDescription", typeof(string)), new DataColumn("ImagePath", typeof(string)) });
            dt.Rows.Add(new object[] { "image 1", "images/image1.png" });
            dt.Rows.Add(new object[] { "image 2", "images/image2.png" });
            dt.Rows.Add(new object[] { "image 3", "images/image3.png" });

            // bind repeater control and data
            ImageRepeater1.DataSource = dt;
            ImageRepeater1.DataBind();
        }

Open in new window

0
 
Tapan PattanaikSenior EngineerAuthor Commented:
Thanks to all.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now