开发者

SharePoint 2010 Web controls - LookupField value not being set on postback

I've created a custom edit form as a simple aspx page in VS2010 (inherits from LayoutsPageBase) which uses the SharePoint Web controls LookupField control to display a drop down list of values from a custom type

The form displays correctly with the drop down box containing the expected range of values

The ControlMode is set to the same as the FormContext (though I have tried explicitly setting this to Edit)

But on the postback the value of the dropdown list is not set - the selected item index is set to -1

How can I correctly use the LookupField control to capture a selected value from the user?

Could it be because I'm adding the controls declaritivly in the aspx and then setting the list id etc from the SPContext in the page load event? - see code snippet below (not the prettiest but just trying to get it to work at this point):

from aspx:

<SharePoint:FileField ID="FileNameText"  InputFieldLabel="Name" runat="server" 开发者_JAVA技巧ControlMode="Display"/><br />
<SharePoint:LookupField ID="FeedType" runat="server" />
<SharePoint:TextField ID="FeedStatus" runat="server"  />

....

in the code behind page load:

if (!IsPostBack)
            {
                SPItem feedFileItem = SPContext.Current.Item;
                FileNameText.ControlMode = SPContext.Current.FormContext.FormMode;
                FileNameText.ListId = SPContext.Current.ListId;
                FileNameText.ItemId = SPContext.Current.ItemId;
                FileNameText.FieldName = "Name";

                FeedType.ControlMode = SPControlMode.Edit;

                FeedType.ListId = SPContext.Current.ListId;
                FeedType.ItemId = SPContext.Current.ItemId;
                FeedType.FieldName = "FeedType";

                FeedStatus.ItemContext = SPContext.Current;
                FeedStatus.RenderContext = SPContext.Current;
                FeedStatus.ControlMode = SPControlMode.Edit;
                FeedStatus.ListId = SPContext.Current.ListId;
                FeedStatus.ItemId = SPContext.Current.ItemId;
                FeedStatus.FieldName = "FeedStatus";
            }

UPDATE

Ok I managed to get my form working by adding the controls in the code behind in the override of CreateChildControls - this is in line with the majority of the samples I've seen on the net.

But can someone explain why my approach didn't work and whether I can do this all in a declarative way in the aspx?


During postbacks selected values from lists are simply ignored if the list control isn't populated. So if you choose item 2 and the list items are null it will simply ignore the response parameter and not set the Value property. This is because ProcessPostData occurs prior to LoadData. Even if you were to remove the !IsPostBack on the LoadData method it still wouldn't work because ProcessPostData still occurs before LoadData and you didn't load the list prior to processing the postback.

A simple way to fix this is move your initialization code into the EnsureChildControls method of your Application page.

protected override void EnsureChildControls()
{
    base.EnsureChildControls();

    ...
    FeedType.ControlMode = SPControlMode.Edit;

    FeedType.ListId = SPContext.Current.ListId;
    FeedType.ItemId = SPContext.Current.ItemId;
    FeedType.FieldName = "FeedType";  
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜