简体   繁体   English

通过代码将数据插入asp.net表

[英]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. 我在asp.net页中有一个表,我想插入数据,这些数据将通过后面的C#代码从服务调用中接收。 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. 我只需要插入从服务调用中获得的值来代替&nbsp。

In your aspx page asp:Label controls and assign the values from code behind by accessing them using Id. 在您的aspx页面中,使用asp:Label控件并通过使用ID访问它们来从代码后面分配值。

Inside .aspx 内.aspx

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

In code behind 在后面的代码中

  lblName.Text = "Value from Service";

If you need to repeat this table use GridView. 如果需要重复此表,请使用GridView。

Use the asp:Table control instead.. It gives you much more control from server side than a normal html tag :) 请改用asp:Table控件。它比普通的html标签从服务器端提供了更多的控制:)

And it ofc render as a normal table client side 并且它将ofc渲染为普通表客户端

If you persist on working with pure html table you can use an new/old style to control it. 如果您坚持使用纯HTML表格,则可以使用新/旧样式进行控制。

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. 如果数据不是动态的,并且您的表不会增长或更改大小,则可以为此使用一些OOP。

like so: create in your class properties and then populate them. 就像这样:在您的类属性中创建然后填充它们。 public string MyLabel { get; 公共字符串MyLabel {get; set; 组; } }

put something in page load. 在页面加载中添加一些内容。

in aspx do it like so.. 在aspx中这样做

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

or 要么

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

Make the table Html server side control. 使表格成为HTML服务器端控件。 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=..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM