0

I am trying to make a dynamic html code through the cs code by using src from a dataset. (every item in the dataset is a src of another picture).It's the first time i am trying to do something like this and it doesn't work, any tips would be helpful :)

html code:

 <asp:Literal ID="imageGallery" runat="server" />

cs (c#) code:

    DataSet ds = new DataSet();
    ds = DAL.GetBestPics();
    imageGallery.Mode = LiteralMode.Encode;
    string divStart = @"<div class='more-products-holder'><ul>";
    imageGallery.Text += divStart;

    foreach (DataRow item in ds.Tables[0].Rows)
    {
        string imageHTML = @"<li><a>
                       <img src='";
        string mid = @"" + item.ToString();
        string imageHTML2 = @"' /></a></li>";
        imageGallery.Text += imageHTML;
        imageGallery.Text += mid;
        imageGallery.Text += imageHTML2;

    }
    string divEnd = @"</ul></div>";
    imageGallery.Text += divEnd;

    this.Controls.Add(imageGallery);
2
  • First, thanks for the fast comments.Nope, only "bad picture" like the source isn't good or something like that Commented Apr 2, 2013 at 10:20
  • I guess the path returned by item.ToString() is not correct. Open the output html and try to browse to the path mention in the img SRC attribute. Commented Apr 2, 2013 at 10:35

3 Answers 3

1
foreach (DataRow item in ds.Tables[0].Rows)
{
    ...
    string mid = @"" + item.ToString();

item is DataRow object. you are suppose to refer to one of the field/column, like:

item["FieldName"].ToString();

Sign up to request clarification or add additional context in comments.

Comments

1

In your code you assign the DataRoW as image Source not DataRow Value..

string mid = @"" + item.ToString(); 

item.ToString() - > is a DataRow Not a DataRow Value

its output was like this <img src='System.Data.DataRow' />

you need to change that like below

item["imageSrc"].ToString() - > it returns DataRow Value

you must assign a field name to that like below

string mid = @"" + item["ImageSRC"].ToString();

its output was like this <img src='sam.jpg' />

Comments

0

What about using stringbuilder class

 StringBuilder sb = new StringBuilder();
        sb.Append("<div class='more-products-holder'><ul>");
        foreach (DataRow item in ds.Tables[0].Rows)
        {
            sb.Append("<li><a><img src='" + item.ToString() + "'");
            sb.Append("' /></a></li>");

        }

       sb.Append("</ul></div>");
       imageGallery.Text = sb.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.