简体   繁体   English

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

[英]paging in gridview control in asp.net

I have one button and one gridview in my asp.net web page.I give the following code in c# code behind file 我的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();

}

and in aspx page 在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>

When i click the button1 it will display the gridview with two rows. 当我单击button1时,它将显示带有两行的gridview。 But when i click the pagenumbers in the grid view like to click page number 2 it will display no data available . 但是,当我单击网格视图中的页面编号(如单击页面编号2)时,它将不显示任何可用数据。 I want to display that page in the grid view. 我想在网格视图中显示该页面。 Can anyone tell how to do this it will really appreciated. 谁能告诉我该怎么做,将不胜感激。 Thank you 谢谢

When 什么时候

You need to add a state to remeber the last data that you like to show, and not make databind on page change, just give the data. 您需要添加一个状态以记住您想要显示的最后一个数据,而不是在页面上进行数据绑定更改,只需提供数据即可。 Try this. 尝试这个。

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;

}

What I have made here. 我在这里做了什么。 I have add a viewstate to rember the last data that you like to show. 我添加了一个viewstate以记录您想要显示的最后一个数据。 So after the button pressed, and then the page pressed, page remeber to call again the same data, but with out databing, on pageload, so page will change. 因此,在按下按钮,然后按下页面之后,页面记住将再次调用相同的数据,但是在页面加载时没有数据存储,因此页面将发生变化。 One think left to do, is to reset the pager if the user press again your button. 剩下要做的一件事是,如果用户再次按下您的按钮,则重置寻呼机。

In the page load use Button1_Click1(dataGrid, EventArgs.Empty); 在页面加载中,使用Button1_Click1(dataGrid, EventArgs.Empty); .When page index changed it is not going to that method.. 当页面索引更改时,它不会使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM