I wanna do this programmatically base on the ListId and ListItemId(for Editing) so, if i wanna use DataFormWebPart i have to set the XSL Property in design mode and i don't know how to reach the XSL of Target List in Runtime and object model. i can change the target list in query string but the interface not gonna change because the XSL property is not defined:
<WebPartPages:DataFormWebPart id="dataForm" runat="server" IsIncluded="True" NoDefaultStyle="TRUE"
ViewFlag="8" Title="TestParnian" PageType="PAGE_EDITFORM" ListName="{8965AE3A-CCA0-4502-B7CE-056CD670C016}" Default="FALSE"
DisplayName="TestParnian" __markuptype="vsattributemarkup" __WebPartId="{2CDB3F60-D76F-4410-AEDA-28D9D5815854}">
<DataSources>
<SharePoint:SPDataSource id="TestParnian2" runat="server" DataSourceMode="ListItem" UseInternalName="true"
UseServerDataFormat="True"
selectcommand="<View><Query><Where><Eq><FieldRef Name="ContentType"/><Value Type="Text">مورد</Value></Eq></Where></Query></View>">
<SelectParameters>
<WebPartPages:DataFormParameter Name="ListItemId" ParameterKey="ListItemId"
PropertyName="ParameterValues" DefaultValue="0"/>
<WebPartPages:DataFormParameter Name="ListID"
ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
<WebPartPages:DataFormParameter Name="MaximumRows" ParameterKey="MaximumRows" PropertyName="ParameterValues"
DefaultValue="10"/>
</SelectParameters>
<InsertParameters>
<WebPartPages:DataFormParameter Name="ListItemId" ParameterKey="ListItemId"
PropertyName="ParameterValues" DefaultValue="0"/>
<WebPartPages:DataFormParameter Name="ListID"
ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
</InsertParameters>
<UpdateParameters>
<WebPartPages:DataFormParameter Name="ListItemId" ParameterKey="ListItemId"
PropertyName="ParameterValues" DefaultValue="0"/>
<WebPartPages:DataFormParameter Name="ListID"
ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
</UpdateParameters>
</SharePoint:SPDataSource>
</DataSources>
<ParameterBindings>
<ParameterBinding Name="ListItemId" Location="QueryString(ID)" DefaultValue="0"/>
<ParameterBinding Name="ListID" Location="QueryString(ListID)" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
<ParameterBinding Name="MaximumRows" Location="None" DefaultValue="10"/>
<ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
<ParameterBinding Name="ManualRefresh" Location="WPProperty[ManualRefresh]"/>
<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
<ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
</ParameterBindings>
<XSL> The XSL Property that needs to be define. </XSL>
</WebPartPages:DataFormWebPart>
So DataFormWebPart not gonna help me.the next option is ListFormWebPart that doesn't need the XSL property and can be create in runtime with the object model like this:
int itemId = listItem != null ? int.Parse(listItem.ID.ToString()) : -1;
ListFormWebPart lstFormList = new ListFormWebPart();
lstFormList.ListName = lst.ID.ToString("B").ToUpper();
if (itemId != -1)
{
lstFormList.ListItemId = itemId;
lstFormList.PageType = PAGETYPE.PAGE_EDITFORM;
lstFormList.ControlMode = SPControlMode.Edit;
lstFormList.TemplateName = "ListForm";
lstFormList.FormType = 6;
lstFormList.Title = "Edit " + lst.Title;
}
else
{
lstFormList.ControlMode = SPControlMode.New;
lstFormList.PageType = PAGETYPE.PAGE_NEWFORM;
lstFormList.TemplateName = "ListForm";
lstFormList.FormType = 8;
lstFormList.Title = "Add " + lst.Title;
}
lstFormList.ListTitle = lst.Title;
lstFormList.AllowClose = false;
lstFormList.AllowConnect = false;
lstFormList.AllowEdit = false;
lstFormList.AllowZoneChange = true;
lstFormList.EnableViewState = true;
lstFormList.HideIfNoPermissions = true;
Panel1.Controls.Add(lstFormList);
this code work nicely in WebPart but not in ApplicationPage and i must say that i added all of Namespaces and References that this webpart needs, to the page.
this give me this error:
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.WebPartPages.ListFormWebPart.OnPreRender(EventArgs e) at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
i put and test this code in Page_Init
, Page_Load
and ListFormWebPart_Init
events but not any success. I use sharepoint designer and create ListFormWebPart
and then copy it and pasted to my Application Page and also add the assembly needed for it, but also not any success.
i don't know what's the problem, please give the right approach if mine isn't.