注册 登录
编程论坛 ASP.NET技术论坛

关于GridView分页问题

百灵鸟 发布于 2008-09-26 21:40, 1441 次点击
public partial class hello_Ex09_03 : System.Web.UI.Page
{
    protected SqlConnection myConnection = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Count == 0)
        {
            Response.Redirect("Ex09_01.aspx");
        }
        else if (!IsPostBack)
        {
            Label1.Text = "热烈欢迎" + Session["username"].ToString();
            string strConn = "server=127.0.0.1;uid=sa;pwd=123;database=Aspnet";
            myConnection.ConnectionString = strConn;
            BindData();
        }

    }

    private void BindData()
    {
        string selCmd = "select * from Ex09_GuestBook where ParentID=0 order by PostTime desc";
        SqlDataAdapter da = new SqlDataAdapter(selCmd, myConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "word");
        GridView1.DataSource = ds.Tables["word"].DefaultView;
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();

    }
}

这个程序当点下一页时为什么会出现异常
异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。

各位请帮忙看一下
5 回复
#2
seaven2008-09-27 10:33
GridView 中AllowPaging 属性改为 true
#3
bygg2008-09-27 17:20
string strConn = "server=127.0.0.1;uid=sa;pwd=123;database=Aspnet";
myConnection.ConnectionString = strConn;

将这两句放到Page_Load()外面
#4
百灵鸟2008-09-27 20:47
回复 3# bygg 的帖子
可以实现分页功能了,太感谢你了。
#5
xiaojie_cp2008-09-28 12:18
觉得最好通过数据库操作类来操作.我也初学,呵呵
#6
bygg2008-10-11 10:59
其实不建议用GridView1.如果数据少可以,但是数据多了就会很慢很慢

因为他的分页是把数据全部读到内存中,再根据不同的页数进行数据分段显示
1