PRB: A NullReferenceException Occurs If You Use the SelectionList Control (313673)



The information in this article applies to:

  • Microsoft Mobile Internet Toolkit (MMIT)

This article was previously published under Q313673

SYMPTOMS

You may receive the following error message when you are browsing a mobile Web Form that accesses the Selection property of a SelectionList control:
System.NullReferenceException: Object reference not set to an instance of an object.

CAUSE

This behavior may occur if the SelectionList is databound on each postback and the event that does the databinding is before the event that attempts to access the Selection property of the SelectionList control. In this case, the Selection property is null because the control has been re-bound. For example, the error is generated if databinding occurs in the Page_Load event, and if the Selection property is accessed in a Click event for a Command control.

RESOLUTION

In most cases, databinding code for the SelectionList should only execute the first time that a page is requested. Additional postbacks continue to populate the control with the ViewState information that is stored in the session state on the Web server. You can use the IsPostBack property of the Page class to prevent the databinding code from executing upon postback.

The following code samples illustrate the use of the IsPostBack property:

Microsoft Visual Basic .NET Sample

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
  If Not Page.IsPostBack Then
    ' Place the Databinding code in here.
  End If
End Sub
				

Microsoft Visual C# .NET Sample

private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack)
  {
    // Place the Databinding code in here.
  }
}
				

STATUS

This behavior is by design.

Modification Type:MajorLast Reviewed:6/14/2002
Keywords:kbDataBinding kbDSupport kbprb kbServerControls KB313673