Solved

binding image control to dataset

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript and Jquery not firing 9 42
Capture logon name 13 42
is this a cms? 8 35
Having problem getting this to work 2 21
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now