0

I have textbox and a button on my aspx page.

Now I want to create a html table as string on button click event. I have done this task. Please look at my below code:

        string stsrtest = "test";
        string nWidth="150px";          
        string strHtml = "<table><tr><td width= '" + nWidth + "'> Authorized By</td><td>Employee </td><td>Status</td><td>Date </td><td>Note</td><td>Signutare</td></tr>";
        strHtml += "<tr><td> " + stsrtest + "</td><td> " + stsrtest + "</td><td> " + stsrtest + "</td><td> " + stsrtest + "</td><td> " + stsrtest + "</td><td>test</td></tr>";
        strHtml += "</table>";

Now I want to create row and column dyanically based on textbox value.

For Example if textbox value is 15 then I want create a table with 3 column and 5 Rows. and if textbox value is 6 then I want create a table with 3 column and 2 Rows.

I have use above code to create a simple 2 rows and 5 column.

1
  • You need to use factorization algorithm for that..... Commented Oct 29, 2013 at 7:07

2 Answers 2

1

I have solved this issue using below code:

        int nTotal = Convert.ToInt32(txtCell.Text);
        int nRows = 0;
        int nLast = nTotal % 3;
        nRows = nTotal / 3;
        string strHmtl = "<table border='1px' cellspacing='0' cellpadding='0'  style='height: 28px; width: 647px;' >";

        for (int i = 0; i < nTotal - nTotal % 3; i++)
        {
            if (i == 0 || i % 3 == 0)
                strHmtl += "<tr>";
            strHmtl += "<td></td>";
            if (i % 3 == 2)
                strHmtl += "</tr>";
        }
        if (nTotal % 3 != 0)
        {
            strHmtl += "<tr>";
            if (nLast == 1)
                strHmtl += "<td> </td>";               
            else
            {
                strHmtl += "<td>  </td>";
                strHmtl += "<td>  </td>";
            }
            strHmtl += "</tr>";
        }
        strHmtl += "</table>";

Thanks.

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

Comments

0

You can just build your table dynamically by setting the number of your columns and looping for each row to build it as string. Take a look at this example:

    StringBuilder l_strBuilder= new StringBuilder();
            l_strBuilder.AppendLine("<table">");
            l_strBuilder.AppendLine("<tr>");
            l_strBuilder.AppendLine("<th>Authorized By</th>");
            l_strBuilder.AppendLine("<th>Employee</th>");
            l_strBuilder.AppendLine("<th>Status</th>");
            l_strBuilder.AppendLine("<th>Date</th>");
            l_strBuilder.AppendLine("<th>Note</th>");
            l_strBuilder.AppendLine("<th>Signutare</th>");
            l_strBuilder.AppendLine("</tr>");

    for (int i = 0; i < ROWSNUMBER; i++)
    {
            l_strBuilder.AppendLine("<tr>");
            l_strBuilder.AppendLine("<td>column 1 data</td>");
            l_strBuilder.AppendLine("<td>column 2 data</td>");
            l_strBuilder.AppendLine("<td>column 3 data</td>");
            l_strBuilder.AppendLine("<td>column 4 data</td>");
            l_strBuilder.AppendLine("<td>column 5 data</td>");
            l_strBuilder.AppendLine("<td>column 6 data</td>");
            l_strBuilder.AppendLine("</tr>");
    }

            l_strBuilder.AppendLine("</table>");

After the building the table you can use the asp literal obj to put your table in your page.

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

And fill it with your string this way:

ltrDynTable.Text = l_strBuilder.ToString();

Hope it will help.

1 Comment

Hi, Thanks for reply but I want to create Row and column based on textbox value which I have mention in my question with example and i also want to give cell value like for cell one its 1,for cell value its 2,...etc.

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.