When using RadGrid's WebUserControl, the DataItem object becomes null when using an ObjectDataSource only to fill ListBox.
Scenario 1 Works - Calling DataSource = __ and DataBind() in Page_Load:
Scenario 2 - DataItem object is null when needed if using ObjectDataSource to bind data to ListBox:
The ObjectDataSource automatically calls the GetCityCollection() method and that is when the DataItem becomes null. Later in the page lifecycle it will have a value again until I force the ListBox to DataBind in attempt to see if I have a value to DataItem (or even access to a HiddenField for that matter!). I want the CustomerId value to filter my query in the SelectMethod of the ObjectDataSource. **NOTE: this is a very simple example and not real-world as I have moved to a play project for testing to resolve the underlying issue of a null DataItem.** I added the PreRender event handlers to show I have access to the DataItem again after it had become null.
In watching the DataItem value throughout the page lifecycle process, this is what I observed:
Page_Load - DataItem has value. {F10}
Sets my HiddenField value. {F10}
Sets my other two Binds. {F10}
ObjectDataSource calls GetCityCollection() and now DataItem object is null.
After that when stepping into the PreRender the DataItem has a value again until calling ListBox.DataBind() where the ObjectDataSource steps into the GetCityCollection() method and DataItem is once again null.
Can anybody explain why DataItem becomes null please?
Thanks in advance!!
-Carrie
Scenario 1 Works - Calling DataSource = __ and DataBind() in Page_Load:
protected void Page_Load(object sender, EventArgs e) { lstItemCollection.DataSource = GetCityCollection(); lstItemCollection.DataBind(); } public IEnumerable<Customer> GetCityCollection() { using (UowEntities uow = new UowEntities()) { CustomerRepository cr = new CustomerRepository(uow); return cr.SelectAll().ToArray(); } }<asp:ListBox ID="lstItemCollection" runat="server" SelectionMode="Multiple" DataValueField="CustomerId" DataTextField="City" />Scenario 2 - DataItem object is null when needed if using ObjectDataSource to bind data to ListBox:
<asp:ListBox ID="lstItemCollection" runat="server" SelectionMode="Multiple" DataValueField="CustomerId" DataTextField="City" DataSourceID="odsCityCollection" /> <asp:ObjectDataSource ID="odsCityCollection" runat="server" SelectMethod="GetCityCollection"TypeName="LINQScratchPad.Web.CommonUserControls.CustomerControl" ></asp:ObjectDataSource>The ObjectDataSource automatically calls the GetCityCollection() method and that is when the DataItem becomes null. Later in the page lifecycle it will have a value again until I force the ListBox to DataBind in attempt to see if I have a value to DataItem (or even access to a HiddenField for that matter!). I want the CustomerId value to filter my query in the SelectMethod of the ObjectDataSource. **NOTE: this is a very simple example and not real-world as I have moved to a play project for testing to resolve the underlying issue of a null DataItem.** I added the PreRender event handlers to show I have access to the DataItem again after it had become null.
public partial class CustomerControl : System.Web.UI.UserControl { private object _dataItem; public object DataItem { get { return this._dataItem; } set { this._dataItem = value; } } protected void Page_Load(object sender, EventArgs e) { }
public IEnumerable<Customer> GetCityCollection() { using (UowEntities uow = new UowEntities()) { Customer foo = (Customer)DataItem; // Always null! // Will throw null reference exception here if uncommented //var bar = HiddenField1.Value; CustomerRepository cr = new CustomerRepository(uow); return cr.SelectAll().ToArray(); } } protected void Page_PreRender(object sender, EventArgs e) { Customer foo = (Customer)DataItem; // Has value here var bar = this.HiddenField1.Value; // Has value here lstItemCollection.DataBind(); // DataItem is now null again :( } protected override void OnPreRender(EventArgs e) { Customer foo = (Customer)DataItem; // Has value here var bar = this.HiddenField1.Value; // Has value here lstItemCollection.DataBind(); // DataItem is now null again :( base.OnPreRender(e); } }In watching the DataItem value throughout the page lifecycle process, this is what I observed:
Page_Load - DataItem has value. {F10}
Sets my HiddenField value. {F10}
<asp:HiddenField Value='<%# Eval("CustomerId") %>' ID="HiddenField1" runat="server" /><ol class="column1"> <li> <asp:Label ID="lblFirstName" runat="server">First Name:</asp:Label> <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' /> </li> <li> <asp:Label ID="lblLastName" runat="server">Last Name: </asp:Label> <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>' /> </li> </ol>After that when stepping into the PreRender the DataItem has a value again until calling ListBox.DataBind() where the ObjectDataSource steps into the GetCityCollection() method and DataItem is once again null.
Can anybody explain why DataItem becomes null please?
Thanks in advance!!
-Carrie