【问题标题】:How to get html table data to a data table如何获取html表数据到数据表
【发布时间】:2017-06-10 04:26:27
【问题描述】:

我正在使用带有 asp 中继器的表。我想将表数据检索到 C# 中的数据表中。我该怎么做?

设计:

    <asp:Repeater runat="server" ID="rptItems">
       <HeaderTemplate>
        <table id="tblDetItems" border="1" style="font-size: 9pt; border-color: #A9A9A9;
        position: relative; overflow-y: auto;" class="display" cellspacing="0">
        <thead style="background: #808080; color: White; font-weight: bold; border-color: White;">
        <tr>
         <th style="width: 10px;">
            SlNo.
         </th>
        <th style="width: 200px;">
            Item Code
        </th>
        <th style="width: 300px;">
            Description
        </th>
        <th style="width: 80px;">
            Group
        </th>
        <th style="width: 100px;">
            Standard Rate
        </th>
        <th style="width: 100px;">
            Labour Charge
        </th>
        <th style="width: 100px;">
            Recovery Cost
        </th>
        <th style="width: 80px;">
            Active ID
        </th>
      </tr>
      <tr style="background-color: Silver;">
        <th style="width: 10px;">
          <input type="text" runat="server" style="width: 60px;" id="txtslno" />
        </th>
        <th style="width: 200px;">
          <input type="text" runat="server" style="width: 200px;" id="txtCode" />
        </th>
        <th style="width: 300px;">
          <input type="text" runat="server" style="width: 300px;" id="txtDesc" />
        </th>
        <th style="width: 80px;">
          <input type="text" runat="server" style="width: 80px;" id="txtGroup" />
        </th>
        <th style="width: 100px;">
          <input type="text" runat="server" style="width: 100px;" id="txtStdRate" />
        </th>
        <th style="width: 100px;">
          <input type="text" runat="server" style="width: 100px;" id="txtLbrCharge" />
        </th>
        <th style="width: 100px;">
          <input type="text" runat="server" style="width: 100px;" id="txtRcvryCost" />
        </th>
        <th style="width: 80px;">
          <select id="cmbUseId" runat="server" style="width: 80px;">
            <option value="Y">Yes</option>
            <option value="N">No</option>
          </select>
        </th>
      </tr>
      </thead>
      <tbody>
    </HeaderTemplate>
      <ItemTemplate>
       <tr>
        <td style="width: 10px; text-align: right; height: 13px;">
          <%# Eval("sl_no")%>
        </td>
        <td style="width: 200px; height: 13px;">
          <input type="text" runat="server" style="width: 190px; text-align: left; height: 13px;" id="txtCode" value='<%# Eval("item_cd")%>' onkeyup="return onkeyup_txtphystkv(this,event);" />
        </td>
        <td style="width: 300px; height: 13px;">
          <%# Eval("item_desc")%>
        </td>
        <td style="width: 80px; height: 13px;">
          <%# Eval("gp_cd")%>
        </td>
        <td style="width: 100px; text-align: right; height: 13px;">
          <input type="text" runat="server" style="width: 100px; text-align: right; height: 13px;" id="txtStdRate" value='<%# Eval("std_rt")%>' onkeyup="return onkeyup_txtphystkv(this,event);" />
        </td>
        <td style="width: 100px; height: 13px;">
          <input type="text" runat="server" style="width: 100px; text-align: right; height: 13px;" id="txtLbrCharge" value='<%# Eval("labour_charge")%>' onkeyup="return onkeyup_txtphystkv(this,event);" />
        </td>
        <td style="width: 100px; height: 13px;">
          <input type="text" runat="server" style="width: 100px; text-align: right; height: 13px;" id="txtRcvryCost" value='<%# Eval("recovery_cost")%>' onkeyup="return onkeyup_txtphystkv(this,event);" />
        </td>
        <td style="width: 80px; text-align: right; height: 18px;">
          <select id="cmbUseId" runat="server" style="width: 80px; height: 18px;">
           <option value="Y" selected="selected">Yes</option>
           <option value="N">No</option>
         </select>
        </td>
      </tr>
      </ItemTemplate>
     <FooterTemplate>
    </tbody> </table>
   </FooterTemplate>
  </asp:Repeater>

【问题讨论】:

  • 您可以在按钮单击上添加C# 代码以创建一个新的空数据表,然后用您的表数据填充它

标签: c# datatable html-table repeater


【解决方案1】:

首先,需要将中继器控件中输入的数据带到服务器。为此,控件(文本框、下拉列表等)应该可以在后面的代码中访问。

你需要替换

<input type="text"> 
<select id="cmbUseId" runat="server">

<asp:TextBox id="<<appropriateId>>" runat="server" />
<asp:DropDownList id="<<dropdownListId1>>" runat="server">
    <asp:ListItem Text="Yes" Value="Y" />
    <asp:ListItem Text="No" Value="N" />
</asp:DropDownList>

单击按钮后,您需要创建一个 DataTable 并向其添加适当的列。

var dataTable = new DataTable();

var column = new DataColumn();
column.ColumnName = <<columnname1>>;
column.DataType = <<columntype1>>;

dataTable.Columns.Add(column);

column = new DataColumn();
column.ColumnName = <<columnname2>>;
column.DataType = <<columntype2>>;

dataTable.Columns.Add(column);

//And So On.. to add necessary columns to the datatable.

然后循环遍历转发器的所有项目,访问每个项目的控件并将它们填充到 dataRow 中,并将数据行添加到上面创建的表中。

foreach (RepeaterItem item in rptItems.Items)
{
    var dataRow = dataTable.NewRow();

    if (item.ItemType == ListItemType.Item)
    {
        var textBox1 = (TextBox)item.FindControl("<<textboxId1>>");

        dataRow["<<columnname1>>"] = textBox1.Text;

        var textBox2 = (TextBox)item.FindControl("<<textboxId2>>");

        dataRow["<<columnname2>>"] = textBox2.Text;

        //And So On... to retrive values from all the textboxes inside the item and set values of appropriate columns in dataRow;

        var dropdownList = (DropDownList)item.FindControl("<<dropdownListId1>>")

        dataRow["<<somecolumn>>"] = dropdownList.SelectedValue;

        //Once values from all the controls of item are obtained and set in the dataRow;

        datatable.Rows.Add(dataRow);
    }
}

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多