DataGrid bound to Array... Postback DataItem is *always* null

R

Ryan Cromwell

Hola

We use a number of Web Services to retreive data and, therefore, have a lot
of proxy objects containing the information we need to display in our web
applications.

For that reason, we are binding a DataGrid to an array of these proxy
objects, which at first, works fine. The problems arise on PostBack when,
even if we rebind to a cached version of the array, the
Datagrid.Items[].DataItem is ALWAYS null.

As a few sites/pages have eluded to, we even tried binding int he
Page.OnInit override, but that doesn't help.

----EXAMPLE CODE----

namespace StupidWebApp
{
public class ArrayBoundDataGridExample : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnTryIt;
protected System.Web.UI.WebControls.DataGrid dgMyGrid;

private void Page_Load(object sender, System.EventArgs e)
{
Person[] people = Session["DUMBYCACHE"] as Person[];

if( people == null )
{
people = new Person[] {
new Person( "Ryan", "Cromwell", new DateTime( 1979, 2, 9 ) ),
new Person( "Nick", "Cromwell", new DateTime( 1987, 4, 14 ) ),
new Person( "Sean", "Cromwell", new DateTime( 1990, 5, 7 ) )
};

Session.Add( "DUMBYCACHE", people );
}

this.dgMyGrid.DataSource = people;
this.dgMyGrid.DataBind();
}

private void btnTryIt_Click(object sender, System.EventArgs e)
{
foreach( DataGridItem item in this.dgMyGrid.Items )
{
switch( item.ItemType )
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
item.BackColor = item.DataItem == null ? Color.Red : Color.Green;
break;
}
}
}

}

public class Person
{
private string _first;
private string _last;
private DateTime _dob;

public Person( string first, string last, DateTime dob )
{
this._first = first;
this._last = last;
this._dob = dob;
}

public string First
{
get { return this._first; }
}

public string Last
{
get
{
return this._last;
}
}

public DateTime DOB
{
get { return this._dob; }
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top