我有一个DataPager
,我用它来浏览搜索结果。我的DataPager失去了它的结果后,我已经点击和页面,虽然其中一些,例如,如果我页2-3次,我只会得到标签像ID和没有数据。
标记:
<asp:ListView runat="server" ID="LVCAdmin">
<!-- Templates here -->
</asp:ListView>
<asp:DataPager ID="DataPager1" PagedControlID="LVCAdmin" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button"
ShowFirstPageButton="True" ShowNextPageButton="False"
ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Button"
ShowLastPageButton="True" ShowNextPageButton="False"
ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
后台代码>: protected void btnSubmit_Click(object sender, EventArgs e)
{
string keyword = txtSearch.Text.Trim();
List<dynamic> Cresults = AdminSearchAll(keyword);
if (Cresults.Count != 0)
{
LVCAdmin.DataSource = Cresults;
LVCAdmin.DataBind();
NoResults.Visible = false;
LVCAdmin.Visible = true;
}
else
{
NoResults.Visible = true;
LVCAdmin.Visible = false;
}
}
我明白了。我在DataPager控件中添加了一个OnPreRender="Pager_PreRender"
。下面是方法。到目前为止,它已按预期工作。
protected void Pager_PreRender(object sender, EventArgs e)
{
if (IsPostBack)
{
string keyword = txtSearch.Text.Trim();
List<dynamic> Cresults = AdminSearchAll(keyword);
if (Cresults.Count != 0)
{
LVCAdmin.DataSource = Cresults;
LVCAdmin.DataBind();
NoResults.Visible = false;
LVCAdmin.Visible = true;
}
else
{
NoResults.Visible = true;
LVCAdmin.Visible = false;
}
}
}