Reputation: 1228
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.
Upvotes: 0
Views: 1241
Reputation: 1228
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.
Upvotes: 1
Reputation: 516
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.
Upvotes: 0