Dynamically in asp.net c # I have a database of books where I have information about a book. I have a line wher...">

Adding an image <img src = "?"> Dynamically in asp.net c #

I have a database of books where I have information about a book. I have a line where I store the image name of the book that I want to display on the page. For example, cprog.jpeg

Now I want to add this name to src in order to display the image. However, this is simply a reflection of the name itself. I'm not quite sure how to do this.

 + " <img src=\"{0} \">" + row[8].ToString() + "</div>"

Here is the complete code.

foreach (DataTable table in dsgrid2.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    strBooksInCategory +=
                          "<div style=\"height:150px;\">"

                          + " <img src=\"{0} \">" + row[8].ToString() + "</div>"
                        + "  <div style=\"height:110px;float:left;padding-left:10px;\">"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[0] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[1] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[2] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[3] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[4] + "</div>"
                        + "   <div style=\"height=auto;left-margin:10px;\">" + row[5] + "</div>"
                        + " </div>"
                        + "</div>";
                    strBooksInCategory += "<div style=\"height:10px;width=100%;\"></div>";
0
source share
3 answers

I assume the string [8] is your image name ... try something like

+ "<img src=\""+ row[8].ToString() +"\"></div>"

You will also need to specify the path to the image, which will look like

+ "<img src=\"/path/to/image/"+ row[8].ToString() +"\"></div>"

... , . - , DataGrids ... !

+1

:

  • . StringBuilder.
  • HTML, . . , , , . , , .
  • , , , Repeater!
  • , URL-, URL-:

    String.Format("http://mysite.com/images/{0}.jpg", row[8]);
    
+1

Please avoid creating HTML in your code. Instead of this:

  • Move all the markup to and from the aspx file.
  • Switch to using controls instead of bare html: <asp:Image>for tag <img>and <asp:Panel>for <div>s
  • Wrap the controls in <asp:Repeater>with<ItemTemplate>
  • Use data binding to assign values ​​from your DataTable to control properties.
0
source

All Articles