Solved

binding image control to dataset

Posted on 2014-01-08
5
532 Views
Last Modified: 2014-01-31
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
Comment
Question by:Tapan Pattanaik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 39764642
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
 
LVL 21

Author Comment

by:Tapan Pattanaik
ID: 39764765
Hi Kishan,

can you share more in depths.
0
 
LVL 7

Accepted Solution

by:
Kishan Zunjare earned 300 total points
ID: 39764787
//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
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 200 total points
ID: 39765302
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
 
LVL 21

Author Closing Comment

by:Tapan Pattanaik
ID: 39825075
Thanks to all.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question