HEEN
HEEN

Reputation: 4721

Displaying the dynamic HTML tr based on datatable records in c# asp.net

I have a requirement where I want to generate dynamic HTML tr rows based on the rows coming from the datatable. Currently from the code which I tried it takes only the first of the datatable and displays in the HTML.

Below is the code which I tried

HTML

<tbody>
    <tr>
        <td align="center" width="4%">{SR_NO}</td>
        <td align="center" width="19%">{MAINTENANCE_POINT}</td>
    </tr>
</tbody>                                                

C#

for (int i = 0; i < dt.Rows.Count; i++)
{
    //outXml = outXml.Replace("{SR_NO}", );  // SR NO
    outXml = outXml.Replace("{MAINTENANCE_POINT}", dt.Rows[i]["ITEM"].ToString());
}

How can I generate to display the multiple tr's based on datatable count?

Update

public void Export()
    {
        string CIRCLE = Request.Form[0];  //Request.Form["circleidSignOff"]
        string HOTODATE = Request.Form[1];

        if (CIRCLE == "GJ")
        {
            CIRCLE = "Gujarat";
        }

        string strCircleHotoDate = CIRCLE + "_" + HOTODATE;
        string strSignOffSheet = CIRCLE + '_' + HOTODATE + ".pdf";

        Document pdfDoc;
        pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);

        MemoryStream ms = new MemoryStream();
        PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);

        SignOffAttrPDF ObjSingOffPDF = new SignOffAttrPDF();
        ObjSingOffPDF.Circle = CIRCLE;
        ObjSingOffPDF.HotoDate = HOTODATE;           
        pdfWriter.PageEvent = ObjSingOffPDF;

        pdfDoc.Open();

        string outXml = string.Empty;            
        string mon = "";

        CLSFilter ObjCLSFilter = new CLSFilter();
        string[] monthArray = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
        int month = Array.IndexOf(monthArray, HOTODATE.Split(' ')[0]);


        if (month <= 10)
        {
            mon = "0" + "" + month;
        }
        else
        {
            mon = "" + month;
        }

        string strDate = mon + "" + HOTODATE.Split(' ')[1];
        DataTable dt = ObjCLSFilter.SignOffSheetData1(CIRCLE, strDate);

        if (dt != null && dt.Rows.Count > 0)
        {
            ObjSingOffPDF.CreatedDateTime = DateTime.Now.ToString();

            using (StreamReader reader = new StreamReader(HttpContext.Server.MapPath("~/ExportToPdf.html")))
            {
                outXml = reader.ReadToEnd();

                outXml = outXml.Replace("{CIRCLE}", CIRCLE);
                outXml = outXml.Replace("{SP_NAME}", CIRCLE);
                outXml = outXml.Replace("{INVENTORY_FOR}", strDate);


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //outXml = outXml.Replace("{SR_NO}", );  // SR NO
                    outXml = outXml.Replace("{MAINTENANCE_POINT}", dt.Rows[i]["ITEM"].ToString());
                }


                reader.Close();
            }
        }

        string[] separator = new string[] { @"<br clear='all' style='page-break-before:always'>" };
        string[] pages = outXml.Split(separator, StringSplitOptions.None);
        int intPageCounter = 0;

        foreach (string page in pages)
        {
            //List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(page), null);

            List<IElement> htmlarraylist = XMLWorkerHelper.ParseToElementList(outXml, null);

            if (intPageCounter > 0)
            {
                pdfDoc.NewPage();
            }

            pdfDoc.SetMargins(20, 20, 60, 40);

            for (int k = 0; k < htmlarraylist.Count; k++)
            {
                pdfDoc.Add((IElement)htmlarraylist[k]);
            }
            intPageCounter++;
        }
        pdfDoc.Close();

        string UploadFolderPath = ConfigurationManager.AppSettings["UploadFolderPath"].ToString();

        string strFilePath = Server.MapPath(UploadFolderPath) + "" + strSignOffSheet + ".pdf" ;

        using (ZipFile zip = new ZipFile())
        {
            zip.AlternateEncodingUsage = ZipOption.AsNecessary;
            if (Directory.Exists(strFilePath))
            {
                DirectoryInfo di = new DirectoryInfo(strFilePath);
                FileInfo[] FileInfo = di.GetFiles();

                if (FileInfo.Length > 0)
                {
                    foreach (FileInfo item in FileInfo)
                    {
                        zip.AddFile(item.FullName, "Files");
                    }
                }
            }

            Stream Objstream = new MemoryStream(ms.ToArray());
            Objstream.Seek(0, SeekOrigin.Begin);

            zip.AddEntry(strSignOffSheet, Objstream);

            HttpContext.Response.Clear();
            HttpContext.Response.BufferOutput = false;
            string zipName = String.Format("Zip_{0}.zip", CIRCLE + "_" + HOTODATE);
            HttpContext.Response.ContentType = "application/zip";
            HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
            zip.Save(HttpContext.Response.OutputStream);
            HttpContext.Response.End();
        }

    }

Upvotes: 0

Views: 191

Answers (1)

Abhishek Pandey
Abhishek Pandey

Reputation: 13558

If you are using MVC, you can use this code in cshtml

<tbody>
  @foreach(item in dt.Rows) {
  <tr>
    <td align="center" width="4%">@item.SR_NO</td>
    <td align="center" width="19%">@item.MAINTENANCE_POINT</td>
  </tr>
  }
</tbody>

More details: Loop through Model items in ASP.NET MVC

Upvotes: 2

Related Questions