0

I have a table in html,

<div id="top-body" class="p-body" style="height:800px; overflow:auto; display:block !important;">
    <table id="o-Table" class="myTable"></table>
</div>

Now I want to add this rows to it, but in page_load method, please guide,

  $("#o-Table").append(
            "<tr class='myRow'>" +
            "   <div class='myRow'>" +
            "       <td class='myCell'  onclick='DoSomething(\"" + someString + "\")'>" +
            "           <div class='myCell2'>" + someString2 + "</div>" +
            "       </td>" +
            "   </div>" +
            "</tr>");
2
  • I only need to figure out, how to dynamically add rows to table in C#, table will be already there Commented Apr 12, 2014 at 16:20
  • I'm not an expert here, but you need to know how to it in jQuery or C# (as title say)? Commented Apr 12, 2014 at 16:28

2 Answers 2

1

You can create HTML elements in your code behind by using HtmlDocument.CreateElement, and then append it to your table with table.AppendChild. More info can be found here

Example:

HtmlElement row = doc.CreateElement("TR");
table.AppendChild(row);

Another option is to use a gridview or listview.

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

2 Comments

what is doc, how to get table ? :S
The table should be the ID of your table element (o-Table). Can't test now but perhaps you can create your row with System.Web.UI.WebControls.TableRow row = new System.Web.UI.WebControls.TableRow();
0

When you tag HTML and C#, I'm gonna guess it's ASP.NET:

Add <%= tableCode %> inside the table to make room for a variable:

<div id="top-body" class="p-body" style="height:800px; overflow:auto; display:block !important;">
    <table id="o-Table" class="myTable">  <%= tableCode %>  </table>
</div>

Have a global variable declared in C# (global meaning outside any method):

public string tableCode;

and on page_load:

tableCode = "<tr class='myRow'>" +
            "   <div class='myRow'>" +
            "       <td class='myCell'  onclick='DoSomething(\"" + someString + "\")'>" +
            "           <div class='myCell2'>" + someString2 + "</div>" +
            "       </td>" +
            "   </div>" +
            "</tr>";

If it's not about ASP.NET please correct me, hope it helps ;)

2 Comments

it is asp.net, but as I described before, I have to add many rows dynamically
By dynamically do you mean with JavaScript?

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.