开发者

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

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

<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. But when i click the pagenumbers in the grid view like to click page number 2 it will display no data available . 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. 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); .When page index changed it is not going to that method..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜