Я использую subsonic и gridview с objectdatasource (раньше я не использовал objectdatasource)
Сетка отлично связывается, моя единственная проблема в том, что я не хочу продолжать вызывать базу данных, чтобы получить счетчик всех записей в таблице. Поэтому, когда я вызываю «FetchCount» (SelectCountMethod) в первый раз, я хочу сохранить его в скрытом поле (или viewstate). По какой-то причине мое скрытое поле всегда пустое, когда я пытаюсь получить к нему доступ, а не значение, действительное скрытое поле. Это также имеет место, если я пытаюсь сохранить его в viewstate.
Это мой осел. Просто сетка, ObjectDatasource и скрытое поле
<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" - это пространство имен веб-страницы, на которой находятся вышеуказанные элементы управления.
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
}
.
Что я делаю не так?
МОЕ РЕШЕНИЕ
Я использовал сессию вместо