源自“asp:Repeater”内部的无法识别的字段错误。

时间:2017-08-17 01:27:56

标签: c# asp.net repeater

我有一个包含多个表单字段的预览页面。以下是其中几个:

<asp:Repeater ID="rptpreview" runat="server">
  <ItemTemplate>
    <table style="width: 100%; border: 1px solid black;">
      <tr>
          <td style="width: 33.3%; border-collapse: collapse; white-space: nowrap;">
              Airport where aircraft primary home based city:
              <asp:Label ID="lblAircraftCity" Text='<%#Eval("aircity") %>'
                 Style="width: 270px; color: #0093B2; font-weight: bold;"
                 runat="server"></asp:Label>
          </td>
       </tr>
    </Table>
  </ItemTemplate>
</asp:Repeater>

我想要做的是将值传递给代码隐藏文件中的这些表单字段,以便用户在提交之前能够查看其条目。以下是代码隐藏文件中的一小部分数据:

int rowIndex = 0;

if (ViewState["CurrentTable"] != null)
{
    //Initialize datatable.
    DataTable ctable = ViewState["CurrentTable"] as DataTable;
    DataTable preview = ctable.Clone();
    int rowNumber = 1;
    //Lets start looping through the second datatable for aircraft schedule info. .
    if (ctable.Rows.Count > 0)
    {
        for (int i = 1; i <= Repeater2.Items.Count; i++)
        {
            DataRow dr = preview.NewRow();
            lblAircraftCity.Text =
                ((TextBox)Repeater2.Items[rowIndex].FindControl("aircraftCity")).Text;
            lblAircraftcnty.Text =
                ((TextBox)Repeater2.Items[rowIndex].FindControl("aircraftcnty")).Text;
            preview.Rows.Add(dr);
            rowNumber++;
            rowIndex++;
        }
        rptpreview.DataSource = preview;
        rptpreview.DataBind();
    }
}

此代码抛出的错误表明某些字段无法识别。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您在Repeater中有一个Label并将其转换为TextBox,这会引发无法识别的字段错误。把它投到Label:

DataRow dr = preview.NewRow();
dr["aircity"] = ((Label)Repeater2.Items[rowIndex].FindControl("aircraftCity")).Text;
dr["aircnty"] = ((Label)Repeater2.Items[rowIndex].FindControl("aircraftcnty")).Text;

这将解决您的问题,lblAircraftCity.TextlblAircraftCnty.Text无法访问Repeater外部,因此您必须分别使用dr["aircity"]dr["aircnty"]