asp.net中gridview控件中的分页
本文关键字:分页 控件 gridview net asp | 更新日期: 2023-09-27 18:00:05
我的asp.net网页中有一个按钮和一个网格视图。我在文件后面的c#代码中给出了以下代码
protected void Button1_Click1(object sender, EventArgs e)
{
string t = @"<countries>
<country>
<name>ANGOLA</name><code>1</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>2</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>3</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>4</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>5</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>6</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>BENIN</name><code>204</code><size>435 amp</size>
</country>
</countries>";
//string bgtFocusCmd = "<bgfocuscmd >";
//string countCmd = "<count name='" + Session["operation"] + "'customerid='" + customerid.Text + "' breakup='" + breakup + "' date='" + DateFrom.Text + "' >";
//bgtFocusCmd += countCmd + "</bgfocuscmd>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(t);
DataSet resultData = new DataSet();
resultData.ReadXml(new StringReader(doc.OuterXml));
dataGrid.DataSource = resultData.Tables[0].DefaultView;
dataGrid.DataBind();
}
和aspx页面
<asp:GridView ID="dataGrid" runat="server"
AllowPaging="True"
AutoGenerateColumns="True"
CellPadding="4"
DataSourceID="Button1_Click1.t"
EmptyDataText="NO data available."
EnableSortingAndPagingCallbacks="true"
ForeColor="#333333"
GridLines="None"
Height="302px"
HorizontalAlign="Left"
PageSize="2"
RowStyle-Width="20"
Width="560px"
OnPageIndexChanged="Button1_Click1">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
当我单击按钮1时,它将显示具有两行的网格视图。但当我点击网格视图中的页码时,就像点击第2页一样,它将显示没有可用的数据。我想在网格视图中显示该页面。有人能告诉我怎么做吗?我真的很感激。非常感谢。
当
您需要添加一个状态来重新标记您喜欢显示的最后一个数据,而不是在页面更改时进行数据绑定,只需提供数据即可。试试这个。
const string cRemStateNameConst = "cRemState_cnst";
public int cRemState
{
set
{
if (value == -1)
ViewState.Remove(cRemStateNameConst);
else
ViewState[cRemStateNameConst] = value;
}
get
{
if (ViewState[cRemStateNameConst] is int)
return (int)ViewState[cRemStateNameConst];
else
return -1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(cRemState == 1)
GetTheData();
}
protected void Button1_Click1(object sender, EventArgs e)
{
cRemState = 1;
GetTheData();
dataGrid.DataBind();
}
void GetTheData()
{
string t = @"<countries>
<country>
<name>ANGOLA</name><code>1</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>2</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>3</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>4</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>5</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>6</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>BENIN</name><code>204</code><size>435 amp</size>
</country>
</countries>";
//string bgtFocusCmd = "<bgfocuscmd >";
//string countCmd = "<count name='" + Session["operation"] + "'customerid='" + customerid.Text + "' breakup='" + breakup + "' date='" + DateFrom.Text + "' >";
//bgtFocusCmd += countCmd + "</bgfocuscmd>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(t);
DataSet resultData = new DataSet();
resultData.ReadXml(new StringReader(doc.OuterXml));
dataGrid.DataSource = resultData.Tables[0].DefaultView;
}
我在这里做的。我添加了一个视图状态,以记住您要显示的最后一个数据。因此,按下按钮后,再按下页面,页面记忆器再次调用相同的数据,但没有数据处理,页面加载时,页面会发生变化。剩下的一个想法是,如果用户再次按下你的按钮,就重置寻呼机。
在页面加载中使用Button1_Click1(dataGrid, EventArgs.Empty);
。当页面索引更改时,它不会使用该方法。。