Chandra Eskay
Chandra Eskay

Reputation: 2203

Insert data into asp.net table through code

I have a table in asp.net page and i want to insert the data which will be recieved from service call through c# code behind. Any idea on how to do this.

Below is my table.

<table id="DataTable" class="style1">
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
</table>

I just need to insert values recieved from service call in place of &nbsp.

Upvotes: 0

Views: 4403

Answers (4)

Mubarek
Mubarek

Reputation: 2689

Make the table Html server side control. Try this:

<table runat="server" id="DataTable" class="style1">
<tr>
    <td id="td1" runat="server">
        &nbsp;</td>
    <td id="td2" runat="server">
        &nbsp;</td>
    <td id="td3" runat="server">
        &nbsp;</td>
    <td id="td4" runat="server">
        &nbsp;</td>
</tr>

Now in the code behind

td1.InnerText="xx" or td1.InnerHtml=..

Upvotes: 1

hackp0int
hackp0int

Reputation: 4169

If you persist on working with pure html table you can use an new/old style to control it.

like so:

<table>
   <% foreach ( var o in objects ) { %>
    <!--UserControl -->
     <tr>
         <td> can put here data like so: <%= o.Id %> </td>
     </tr>  
     <!--UserControl -->
   <%}%>
</table>

or you can use Repeater and Bind data if it's dynamic.

If data is not dynamic and your table will not grow or change size, you can use a little OOP for this.

like so: create in your class properties and then populate them. public string MyLabel { get; set; }

put something in page load.

in aspx do it like so..

<table>
     <tr>
         <td> <%= MyLabel %> </td>
     </tr>           
</table>

or

    <table>
         <tr>
             <td> <asp:Label ID=|"myText" runat="server"/> </td>
         </tr>           
    </table>

Upvotes: 1

Amar Palsapure
Amar Palsapure

Reputation: 9680

In your aspx page asp:Label controls and assign the values from code behind by accessing them using Id.

Inside .aspx

  <asp:Label Id="lblName" runat="server">

In code behind

  lblName.Text = "Value from Service";

If you need to repeat this table use GridView.

Upvotes: 2

f2lollpll
f2lollpll

Reputation: 1007

Use the asp:Table control instead.. It gives you much more control from server side than a normal html tag :)

And it ofc render as a normal table client side

Upvotes: 1

Related Questions