I am trying to load ListFieldIterator control dynamically and it is not working as expected.
Step1: Created Application Page
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationPage1.aspx.cs" Inherits="TaskList.Layouts.Utilities.ApplicationPage1"
DynamicMasterPageFile="~masterurl/default.master" %>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<SharePoint:ListFieldIterator ID="listFieldIterator" ControlMode="New" runat="server" ></SharePoint:ListFieldIterator>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page
</asp:Content>
Code behind
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace TaskList.Layouts.Utilities
{
public partial class ApplicationPage1 : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
if (listFieldIterator.ListId == Guid.Empty)
{
string listName = Convert.ToString(this.Page.Request.QueryString["ListName"]);
if (!string.IsNullOrEmpty(listName))
{
listFieldIterator.ListId = SPContext.Current.Web.Lists[Convert.ToString(this.Page.Request.QueryString["ListName"])].ID;
}
switch (Convert.ToString(this.Page.Request.QueryString["ControlMode"]))
{
case "New":
listFieldIterator.ControlMode = SPControlMode.New;
break;
case "Display":
listFieldIterator.ControlMode = SPControlMode.Display;
break;
case "Edit":
listFieldIterator.ControlMode = SPControlMode.Edit;
break;
default:
listFieldIterator.ControlMode = SPControlMode.New;
break;
}
}
}
}
}
I am passing two values from query string 1. ListName 2. ControlMode
Whatever value I pass it gives me below error message
[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.SharePoint.WebControls.ListFieldIterator.CreateChildControls() +167
System.Web.UI.Control.EnsureChildControls() +145
Microsoft.SharePoint.WebControls.TemplateBasedControl.OnLoad(EventArgs e) +61
System.Web.UI.Control.LoadRecursive() +65
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Control.LoadRecursive() +190
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427
If I hard code ListID on HTML page it works fine but when I set it dinamically its giving me above mentioned error. While doing debugging I came to know that ListFieldIterator control's field collection is not being populated and because of that control is not being rendered.