This is a migrated thread and some comments may be shown as answers.

WebUserControl - DataItem null for ListBox when using ObjectDataSource

1 Answer 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AdventurGurl
Top achievements
Rank 1
AdventurGurl asked on 05 Feb 2011, 06:46 PM
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:

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" />
Sets my other two Binds. {F10}
<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>
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

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 10 Feb 2011, 10:39 AM
Hi AdventurGurl,

The DataItem object for a grid item is available during it is bound. Therefore I suggest that you handle the user control DataBinding event to bind the ListBox. Check the below demo for a reference:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/usercontroleditform/defaultcs.aspx

Best wishes,
Iana
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
AdventurGurl
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or