Blogger

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items

Login

Friday, 21 December 2012 08:56

Empty list item values when using a ListFormWebPart

Written by
Rate this item
(0 votes)

At work, I've develop an User Control that add a ListFormWebPart for adding answers to a Survery List into a publishing page.

In page view, the webpart was rendered correctly and when I click on Add button (to answer the survey), all went well except that the list item that was addes has empty values o.O

{codecitation}

ListFormWebPart lfw = new ListFormWebPart();
lfw.ID = "SurveyWP";
lfw.Title = "";
lfw.ListId = survey.ID;
lfw.ListItemId = 0;
lfw.PageType = PAGETYPE.PAGE_NEWFORM;
lfw.ViewFlags = SPViewFlags.Default;

 

this.Page.Controls.AddAt(lfw,2);

{/codecitation}

The error is that to add the WebPart into the page, I've used at 9th line the AddAt method to add the WebPart at 2nd position.

Using this method to add the WebPart into the page, the system loses the values specified by the users.

The workaround is to use Add method instead AddAt method ad use a div to add the WebPart into a specified position in the page.

{codecitation}

this.containerDiv.Controls.Add(lfw);

{/codecitation}

 

Read 28195 times Last modified on Sunday, 12 January 2014 17:05

3 comments

  • Comment Link Sergio Wednesday, 23 January 2013 16:32 posted by Sergio

    Hi Luca, very nice post.

    I'm doing something similar trying to load a ListFormWebPart to create discussion list posts in a custom form.

    I'm getting System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.WebPartPages.ListFormWebPart.OnPreRender(EventArgs e) with a code like yours.

    I have used reflector to see what happens on that method and think is a problem with an access to SPContext.Current.List inside which I think is null.

    Did you face this problem with your code?
    Thanks in advance.

    Report
  • Comment Link Luca Costante Saturday, 26 January 2013 21:29 posted by Luca Costante

    Hi Sergio.
    I not have your problem because the WP founds the List and it can renders the list (infact at 4th line I set the list ID to the WP).
    In your scenario, I think that it can be null if the WP and the list are on different webs.

    Report
  • Comment Link home page Friday, 12 September 2014 05:56 posted by home page

    Perfectly composed subject material, Really enjoyed looking through.

    Report

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.