Home > ASP.NET, Share Point, SharePoint 2010 > SharePoint: LookupField bug in SharePoint 2010

SharePoint: LookupField bug in SharePoint 2010

February 28th, 2011 Admin Leave a comment Go to comments

    This post is about the same LookupField bug I faced in SharePoint 2007 and described here
http://dotnetfollower.com/wordpress/2011/02/sharepoint-lookupfield-bug/. Briefly, LookupField doesn’t save selected value after an “idle postback”. I’ve analyzed the code of LookupField control from SharePoint 2010, the bug still remains. The steps to reproduce are absolutely identical. They have changed a little bit method SetFieldControlValue (method, where the problem was), now it looks like the following:

private void SetFieldControlValue(object value)
{
    if ((this.m_value != value) || !this.m_hasValueSet)
    {
        this.Clear();
        this.m_value = value;
        this.m_hasValueSet = true;
        if (this.DataSource != null)
        {
            // some code is skipped
            if (this.m_tbx != null)
            {
                DataRowView view = null;
                if (this.m_selectedValueIndex >= 0)
                {
                    view = this.m_dataSource[this.m_selectedValueIndex];
                    this.m_tbx.Text = view["TextField"] as string;
                }
                if (this.Page != null)
                {
                    string str = "0";
                    if (this.m_selectedValueIndex >= 0) // (***) here is the problem
                    {
                        view = this.m_dataSource[this.m_selectedValueIndex];
                        str = ((int)view["ValueField"]).ToString(CultureInfo.InvariantCulture);
                    }
                    else if (this.Page.IsPostBack) // get picked value only if option stored in SPListItem is invalid (m_selectedValueIndex < 0)
                    {
                        str = this.Context.Request.Form[this.HiddenFieldName];
                        if (string.IsNullOrEmpty(str))
                        {
                            str = "0";
                        }
                    }
                    this.Page.ClientScript.RegisterHiddenField(this.HiddenFieldName, str);
                }
            }
        }
    }
}

     Despite changes, they still use m_selectedValueIndex to detect whether they should get value from the hidden html-feild or not. But I’m repeating myself, m_selectedValueIndex reflects the option stored in SPListItem, we shouldn’t take m_selectedValueIndex into account here. When SPListItem is being saving, SharePoint uses the Value property to get the option picked by user on the page. It’s more interesting that inside Value property they rightly get the value from hidden html-field and don’t analyze m_selectedValueIndex.

public override object Value
{
    get
    {
        this.EnsureChildControls();
        if (this.m_tbx != null)
        {
            if (this.Page.IsPostBack) // if it's postback, always get the picked value
            {
                string str = this.Context.Request.Form[this.HiddenFieldName];
                return (string.IsNullOrEmpty(str) ? 0 : int.Parse(str, CultureInfo.InstalledUICulture));
            }
            return  ((this.m_selectedValueIndex >= 0) ? this.m_selectedValueIndex : 0);
        }
        // some code is skipped
    }
}

     To eliminate the bug you still can use FixedLookupField from the previous post http://dotnetfollower.com/wordpress/2011/02/sharepoint-lookupfield-bug/

Related posts:
 
  1. No comments yet.
  1. No trackbacks yet.