Question

I am using subsonic and a gridview with objectdatasource (I have not used an objectdatasource before)

The grid binds just fine, my only problem is that I don't want to keep calling the database to get a count of all the records in the table. So when I call "FetchCount" (the SelectCountMethod) the first time I want to store it ina hiddenfield (or viewstate). For some reason my hiddenfield is always null when I try and access it, not the value, the actual hidden field. This is also the case if I try storing it in the viewstate.

This is my aspx. Just a gridview, ObjectDatasource and a hiddenfield

    <asp:GridView ID="grdResults" runat="server" AllowPaging="True"  AutoGenerateColumns="false"
 DataSourceID="PropertiesDataSource" PageSize="10" >
 <Columns >
 <asp:BoundField DataField="PropertyName" />
 </Columns>
 </asp:GridView>

<asp:ObjectDataSource ID="PropertiesDataSource" runat="server" SelectMethod="FetchPagedData" 
TypeName="TestWebsite.Usercontrols.Search.SearchResults" SelectCountMethod="FetchCount" 
StartRowIndexParameterName="start" 
MaximumRowsParameterName="pageLength" EnablePaging="True" />

<asp:HiddenField ID="hdnTotalRecords" runat="server" />

TypeName="TestWebsite.Usercontrols.Search.SearchResults" is the namespace of the webpage that the above controls are on.

 public int? TotalRecords
    {
        get
        {
            if (hdnTotalRecords.Value != string.Empty) return int.Parse(hdnTotalRecords.Value);
            else return null;
        }
        set { hdnTotalRecords.Value = value.ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
             grdResults.DataBind();

    }

    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public PropertyXCollection FetchPagedData(int start, int pageLength)
    { 
        int startIndex;
        if (start == 0)
            startIndex = 1;
        else           
            startIndex = start / pageLength + 1;

        PropertyXCollection collection = GetProperties(startIndex, 10);
        return collection;
    }

    public int FetchCount()
    {
        int returnVal = 0;
        if (TotalRecords != null)
            returnVal = (int)TotalRecords;
        else
        {
            TotalRecords = GetProperties(null, null).Count;
            returnVal = (int)TotalRecords;
        }
        return (int)returnVal;
    }

    PropertyXCollection GetProperties(int? pageIndex, int? pageCount)
    {
        //method that uses subsonic to return a collection of Properties from the database //and passes in the page index and count
    }

.

What am I doing wrong?

MY SOLUTION

I used the session instead

Was it helpful?

Solution

That's because you're trying to use SearchResults as both a Page and the underlying class backing up the ObjectDataSource.

As explained in the documentation, since your data object methods are not static, the ObjectDataSource will create a new instance of the SearchResults class and call the methods on that instance. This means the hidden field will always be null and the view state contents will be meaningless.

You can persist the record count in the ASP.NET session state instead, which you can access from the current HTTP context:

public int? TotalRecords
{
    get {
        return (int?) HttpContext.Current.Session["TotalRecords"];
    }
    set {
        HttpContext.Current.Session["TotalRecords"] = value;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top