我的ASP页面代码:
<asp:ObjectDataSource runat="server" ID="odsResults" OnSelecting="odsResults_Selecting" />
<tr><td>
<wssawc:SPGridViewPager ID="sgvpPagerTop" runat="server" GridViewId="sgvConversionResults" />
</td></tr>
<tr>
<td colspan="2" class="ms-vb">
<wssawc:SPGridView
runat="server"
ID="sgvConversionResults"
AutoGenerateColumns="false"
RowStyle-CssClass=""
AlternatingRowStyle-CssClass="ms-alternating"
/>
</td>
</tr>
班级代码:
public partial class Convert : System.Web.UI.Page
{
...
private DataTable resultDataSource = new DataTable();
...
protected void Page_Init(object sender, EventArgs e)
{
...
resultDataSource.Columns.Add("Column1");
resultDataSource.Columns.Add("Column2");
resultDataSource.Columns.Add("Column3");
resultDataSource.Columns.Add("Column4");
...
odsResults.TypeName = GetType().AssemblyQualifiedName;
odsResults.SelectMethod = "SelectData";
odsResults.SelectCountMethod = "GetRecordCount";
odsResults.EnablePaging = true;
sgvConversionResults.DataSourceID = odsResults.ID;
ConversionResultsCreateColumns();
sgvConversionResults.AllowPaging = true;
...
}
protected void btnBTN_Click(object sender, EventArgs e)
{
// add rows into resultDataSource
}
public DataTable SelectData(DataTable ds,int startRowIndex,int maximumRows)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Columns.Add("Column4");
for (int i =startRowIndex; i<startRowIndex+10 ;i++)
{
if (i<ds.Rows.Count)
{
dt.Rows.Add(ds.Rows[i][0].ToString(), ds.Rows[i][1].ToString(),
ds.Rows[i][2].ToString(), ds.Rows[i][3].ToString());
}
}
return dt;
}
public int GetRecordCount(DataTable ds)
{
return ds.Rows.Count;
}
protected void odsResults_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["ds"] = resultDataSource;
}
}
单击按钮resultDataSource
会收到一些行。页面重新加载,我们可以在sgvConversionResults
中看到结果。前10行。但是在点击寻呼机的下一页后,我们会收到消息“此视图中没有要显示的项目”。当我尝试调试时,我发现在postBack
页面后(点击下一页)输入参数“ds”为空,ds.Rows.Count = 0
等等......好像resultDataSource
变为空((
我做错了什么?
onPostBack所有变量都获得默认值,sgvConversionResults保存她的结构但是有明确的行。如何保存sgvConversionResults数据onPostBack事件???
答案 0 :(得分:1)
也许试试:
protected void Page_Load(object sender, EventArgs e)
{
sgvConversionResults.DataBind();
}
您的代码似乎有点费解,您使用ObjectDataSource的任何特殊原因?因为您可以将数据表直接绑定到代码隐藏
中的gridview答案 1 :(得分:1)
我使用ViewState传输resultDataSource并且此工作成功!