Search code examples
c#asp.netrepeater

Unrecognized field errors originating from within an 'asp:Repeater'


I have a preview page with several form fields. Below are just a couple of them:

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

What I would like to do is pass values to these form fields from a code-behind file so that users will be able to review their entries before submitting. Here is a small sample of data from the code-behind file:

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();
    }
}

This code is throwing errors that indicate that some of the fields are unrecognized. Can anyone help?


Solution

  • You have a Label inside Repeater and casting it to TextBox which raising the unrecognized fields errors. Cast it to Label:

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

    This will solve your problem as lblAircraftCity.Text and lblAircraftCnty.Text has not access outside the Repeater so you have to use dr["aircity"] and dr["aircnty"] repectively.