有时候我们可以直接从后台生成一些值缓存到客户端,在用到的时候无需在进行callback进行取值,减少和服务器的交互。下面的例子缓存列"title_id"和"title"的值。
<dxwgv:aspxgridview id="grid" runat="server" width="950px" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" KeyFieldName="title_id"
OnCustomJSProperties="grid_CustomJSProperties">
<SettingsPager PageSize="5" />
<Columns>
<dxwgv:GridViewDataTextColumn Caption="title_id" FieldName="title_id" ReadOnly="True"
VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="title" FieldName="title" VisibleIndex="2">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="type" FieldName="type" VisibleIndex="3">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataDateColumn Caption="pubdate" FieldName="pubdate" VisibleIndex="4">
</dxwgv:GridViewDataDateColumn>
<dxwgv:GridViewDataTextColumn Caption="price" FieldName="price" VisibleIndex="5">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="button" VisibleIndex="6">
<DataItemTemplate>
<input type="button" value="Click Me" onclick="ProcessRow(<%# Container.VisibleIndex %>);" />
</DataItemTemplate>
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:aspxgridview>
<script type="text/javascript">
//读取缓存的值
function ProcessRow(index) {
alert("The row id is '" + grid.cpTitleId[index - grid.visibleStartIndex] + "', and title is " + grid.cpTitles[index - grid.visibleStartIndex]);
}
</script>
//后台缓存代码
protected void grid_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
{
int startIndex = grid.PageIndex * grid.SettingsPager.PageSize;
int end = Math.Min(grid.VisibleRowCount, startIndex + grid.SettingsPager.PageSize);
object[] titleId = new object[end - startIndex], titles = new object[end - startIndex];
for (int n = startIndex; n < end; n++)
{
titleId[n - startIndex] = grid.GetRowValues(n, "title_id");
titles[n - startIndex] = grid.GetRowValues(n, "title");
}
e.Properties["cpTitleId"] = titleId;
e.Properties["cpTitles"] = titles;
}