C# asp.net中gridview控件中的分页

C# asp.net中gridview控件中的分页,c#,asp.net,C#,Asp.net,在我的asp.net网页中有一个按钮和一个gridview protected void Button1_Click1(object sender, EventArgs e) { string t = @"<countries> <country> <name>ANGOLA</name><code>1</code><size>1345 amp</si

在我的asp.net网页中有一个按钮和一个gridview

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时,它将显示两行的gridview。 但是,当我单击网格视图中的页码时,就像单击页码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;

}

我在这里做的。我添加了一个viewstate来记住您想要显示的最后一个数据。所以按下按钮后,再按下页面,页面记住再次调用相同的数据,但是没有数据,在页面加载时,页面会发生变化。剩下要做的一件事是,如果用户再次按下按钮,则重置寻呼机。

在页面加载中,使用按钮1\u单击1数据网格,EventArgs.Empty。当页面索引更改时,它将不会使用该方法。

它将正确地给出解决方案。但我需要回邮的帮助。你能告诉我这可能吗?@sanakkian我不明白。你能再告诉我一次你想要什么吗?我需要从webservice获取数据。所以我需要做ISPOSTBack事件。If is postback false表示单击按钮时来自webservice的数据将进入datagrid。If is true表示从页面索引值获取的数据。你能帮我做这件事吗?@sanakkian你甚至不投票给我你不选择答案,有什么帮助吗?