Take the 2-minute tour ×

I am using a LinqDataSource and a FormView with paging enabled on an ASP.NET page. I am trying to access the FormView's DataItem property on PageLoad and I have no trouble on the first page load, but as soon as I use the Next/Prev page button (causing a postback) on the FormView the DataItem property is null, even if there a record showing in the FormView. Any ideas why it works fine on the first page load but not on a postback?

If you're curious what my PageLoad event looks like, here it is:

protected void Page_Load(object sender, EventArgs e)
    Label lbl = (Label)fvData.FindControl("AREALabel");
    if (fvData.DataItem != null && lbl != null)
        var area = db.AREAs.SingleOrDefault(q => q.AREA1 == record.AREA);
        if (area != null)
            lbl.Text = area.AREA_NAME;
share|improve this question
probably you have to rebind your formview in page_index changing event –  Waqar Janjua Jul 18 '12 at 17:47
How can you rebind it if it's using data from a LinqDataSource control I dropped on the page? I've called DataBind() on both the FormView and LinqDataSource, but neither seem to work. –  Hoff Jul 18 '12 at 18:49

2 Answers 2

up vote 4 down vote accepted

The object you bind to any data-bound control won't be persisted in the page's ViewState

Therefore, on subsequent posts the DataItem property will be null unless you re-bind the control

This property will contain a reference to the object when the control is bound.

Usually you would need to access this property if you want to do something when the objects is bound, so you need to react to the DataBound event



enter image description here

Code behind

protected void ds_DataBound(object sender, EventArgs e)
    var d = this.fv.DataItem as employee;
    this.lbl.Text = d.lname;


    <asp:LinqDataSource ID="lds" runat="server"

    <asp:FormView runat="server" ID="fv" DataSourceID="lds" AllowPaging="true" 
            <asp:TextBox Text='<%# Bind("fname") %>' runat="server" ID="txt" />
    <br />
share|improve this answer
Worked like a charm. Using the DataBound event instead of the PageLoad event did the trick. Thank you! –  Hoff Jul 18 '12 at 18:56

Your data will not be preserved on PostBack. You'll need to rebind the FormView in the PageIndexChanging event using something like:

protected void FormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
    FormView.PageIndex = e.NewPageIndex;
    //rebind your data here
share|improve this answer
I'm just using the LinqDataSource control dropped on the page, how can I force it to rebind the data? The paging seems to work fine and I've called DataBind() on both the FormView and LinqDataSource, but neither seem to work. –  Hoff Jul 18 '12 at 18:19
You'd want to DataBind your FormView. Maybe try 'EnableViewstate="true"' in your FormView? –  gws2 Jul 18 '12 at 18:40
EnableViewState is defaulted to true. Very weird behavior because when I page I can clearly see the data in the form view - I just can't access the DataItem :( –  Hoff Jul 18 '12 at 18:47

Your Answer


