Solved

binding image control to dataset

Posted on 2014-01-08
5
503 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now