0

我的 ASP.NET 页面显示了部分databound网格视图:

<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:BoundField DataField="name" HeaderText="Name" />
    <asp:TemplateField HeaderText="Datum" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="datum" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Uhrzeit" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="uhrzeit" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Ort" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="ort" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Hausnummer" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="hausnummer" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Gebäude" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="gebaeude" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Raumnummer" ItemStyle-CssClass="tdRight">
      <ItemTemplate>
        <asp:TextBox ID="raumnummer" runat="server" Text=""></asp:TextBox>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

这些TextBoxes将用用户输入填充,然后作为列添加到 a DataTable

DataTable inputData = new DataTable();
inputData.Columns.Add(new DataColumn("datum", typeof(String)));
inputData.Columns.Add(new DataColumn("uhrzeit", typeof(String)));
inputData.Columns.Add(new DataColumn("ort", typeof(String)));
inputData.Columns.Add(new DataColumn("hausnummer", typeof(String)));
inputData.Columns.Add(new DataColumn("gebaeude", typeof(String)));
inputData.Columns.Add(new DataColumn("raumnummer", typeof(String)));
foreach(DataRow row in myGridView.Rows) {
  foreach(DataColumn col in row.ItemArray) {
    DataRow dr = inputData.NewRow();
    dr["datum"] = ((TextBox) myGridView.FindControl("datum")).Text;
    dr["uhrzeit"] = ((TextBox) myGridView.FindControl("uhrzeit")).Text;
    dr["ort"] = ((TextBox) myGridView.FindControl("ort")).Text;
    dr["hausnummer"] = ((TextBox) myGridView.FindControl("hausnummer")).Text;
    dr["gebaeude"] = ((TextBox) myGridView.FindControl("gebaeude")).Text;
    dr["raumnummer"] = ((TextBox) myGridView.FindControl("raumnummer")).Text;
    inputData.Rows.Add(dr);
    printRow(dr);
  }
}

如何访问textareas,从每个获取文本并将输入数据附加到DataTable用于创建每一行的?

4

1 回答 1

2

您可以循环网格中的所有行,然后用于row.FindControl获取TextBoxes。GridViewRow是控件的NamingContainer不是GridView

foreach (GridViewRow row in myGridView.Rows)
{
    DataRow newRow = inputData.Rows.Add();
    newRow.SetField("datum", ((TextBox)row.FindControl("datum")).Text);
    // and so on...
    // note that you don't need to add the row since it's already added
}
于 2014-09-23T13:59:01.700 回答