簡體   English   中英

通過代碼將數據插入asp.net表

[英]Insert data into asp.net table through code

我在asp.net頁中有一個表,我想插入數據,這些數據將通過后面的C#代碼從服務調用中接收。 關於如何執行此操作的任何想法。

下面是我的桌子。

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

我只需要插入從服務調用中獲得的值來代替&nbsp。

在您的aspx頁面中,使用asp:Label控件並通過使用ID訪問它們來從代碼后面分配值。

內.aspx

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

在后面的代碼中

  lblName.Text = "Value from Service";

如果需要重復此表,請使用GridView。

請改用asp:Table控件。它比普通的html標簽從服務器端提供了更多的控制:)

並且它將ofc渲染為普通表客戶端

如果您堅持使用純HTML表格,則可以使用新/舊樣式進行控制。

像這樣:

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

或者您可以使用中繼器和綁定數據(如果它是動態的)。

如果數據不是動態的,並且您的表不會增長或更改大小,則可以為此使用一些OOP。

就像這樣:在您的類屬性中創建然后填充它們。 公共字符串MyLabel {get; 組; }

在頁面加載中添加一些內容。

在aspx中這樣做

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

要么

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

使表格成為HTML服務器端控件。 嘗試這個:

<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>

現在在后面的代碼中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM