如何添加按钮单击以显示数据库中的下一项

时间:2014-12-21 16:12:24

标签: c# asp.net

这是我的代码,但它不起作用

protected void Button2_Click(object sender, EventArgs e)
    {
        int i = 1;
        if (GridView1.Rows.Count > 0)
        {
            TextBox1.Text = GridView1.Rows[i].Cells[1].Text;
            TextBox2.Text = GridView1.Rows[i].Cells[2].Text;
            TextBox3.Text = GridView1.Rows[i].Cells[31].Text;
            TextBox4.Text = GridView1.Rows[i].Cells[5].Text;
            i++; 
        }
    }

它没有下一个项目。

1 个答案:

答案 0 :(得分:0)

使用此代码

protected void Button2_Click(object sender, EventArgs e)
{
            int i = ViewState["cnt"]!=null? (int) ViewState["cnt"]:1;
            i = i + 1;
            TextBox1.Text = GridView1.Rows[i].Cells[1].Text;
            TextBox2.Text = GridView1.Rows[i].Cells[2].Text;
            TextBox3.Text = GridView1.Rows[i].Cells[31].Text;
            TextBox4.Text = GridView1.Rows[i].Cells[5].Text;
            ViewState["cnt"] = i;
}