在asp.net中的gridview控件中进行分页

时间:2011-06-22 10:19:37

标签: c# asp.net

我的asp.net网页上有一个按钮和一个gridview。我在文件后面的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>

当我点击button1时,它将显示两行的gridview。 但是,当我单击网格视图中的页面编号时,如果单击页面编号2,它将不显示任何可用数据。我想在网格视图中显示该页面。任何人都可以告诉如何做到这一点,它真的很感激。 谢谢                 

何时

2 个答案:

答案 0 :(得分:0)

您需要添加一个状态来记住您想要显示的最后一个数据,而不是在页面更改时使数据绑定,只需提供数据。试试这个。

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;

}

我在这里做了什么。我添加了一个视图状态来重新显示您想要显示的最后一个数据。因此,按下按钮,然后按下页面,页面记忆再次调用相同的数据,但没有数据处理,在页面加载,所以页面将更改。如果用户再次按下按钮,则可以重新设置寻呼机。

答案 1 :(得分:0)

在页面加载中使用Button1_Click1(dataGrid, EventArgs.Empty);。当页面索引发生变化时,它不会使用该方法..