【问题标题】:GridView Paging ProblemGridView 分页问题
【发布时间】:2011-10-22 05:31:04
【问题描述】:

我有一个设置为进行分页的gridview,但它不能正常工作。

只有第一个页面的控件是可见的 - 其他页面呈现了框,但其中没有控件。

有人知道为什么会这样吗?我检查过我有超过一页的数据。

谢谢,

奥利弗

我附上了一张截图,希望能说明我的问题。

http://i.stack.imgur.com/NOFnB.jpg

编辑:gridview 的来源

    <asp:GridView ID="GridView1" runat="server" OnPageIndexChanging="GridView1_PageIndexChanging" 
    CssClass="GridView1" OnSelectedIndexChanged="GridView_SelectedIndexChanged"
    AllowPaging="True" PageSize="20">      
    <selectedrowstyle backcolor="LightCyan" forecolor="DarkBlue" font-bold="true" />
</asp:GridView>

使用 c# 生成的数据集填充它

编辑:c# 代码隐藏

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

    public void bindGridView()
    {
        //declare the connection string to use
        string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

        //create sql connection
        SqlConnection mySQLconnection = new SqlConnection(connectionString);

        //open connection
        mySQLconnection.Open();

            //define command using text string
            SqlCommand mySqlCommand = new SqlCommand(sqlTester, mySQLconnection);
            SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);

            //create dataset to fill gridview with
            DataSet myDataSet = new DataSet();
            mySqlAdapter.Fill(myDataSet);

            //fill gridview
            GridView1.DataSource = myDataSet;
            GridView1.DataBind();

        //close the sql connection
        mySQLconnection.Close();
    }

【问题讨论】:

  • 您能否展示一下用于将数据集绑定到gridview 的代码?
  • 哦,您是否有机会处理 PageIndexChanged 事件?
  • 你检查过源代码吗?数字也没有出现在那里吗?我想知道它是否是未选择页码的 CSS 问题。
  • 你去 - 我的数据集是使用字符串 sqlTester 创建的,我有一些希望处理 pageindexchange 事件的代码 - 我应该处理 pageindexchanged 事件吗?
  • @curt 我看过了,它似乎正在生成 - 有 10 行,每行都有一个 href 链接

标签: c# asp.net gridview paging


【解决方案1】:

我认为根据您的aspx 和您的.cs。您的 css 文件“GridView1”中存在一些问题。尝试删除 css 类并告诉我们。问题是否仍然存在。

注意:

这与您的问题无关。但我认为你应该把你的代码分层而不是把所有的代码都写在你的页面代码后面。

阅读:

  • asp.net 中的分层。
  • ORM.

【讨论】:

  • 删除 css 类有效!我想我会尝试重新开始工作并尝试找出我搞砸的地方。感谢您的提示 - 我会看看分层
  • 不客气,尝试解决 CSS 文件中的问题。如果对您有帮助,请随时签名作为答案。谢谢 :)
  • 现在得到它 - 我有另一块未使用的 css 漂浮在周围,这把事情搞砸了......
  • 很高兴。我很高兴你明白了。
【解决方案2】:

您正在对每个页面索引更改事件进行连接和数据库访问。它会导致大量开销。试试这个。

public void bindGridView()
{
    //declare the connection string to use
    string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

    //create sql connection
    SqlConnection mySQLconnection = new SqlConnection(connectionString);

    //open connection
    mySQLconnection.Open();

        //define command using text string
        SqlCommand mySqlCommand = new SqlCommand(sqlTester, mySQLconnection);
        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);

        //create dataset to fill gridview with
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);

        //fill gridview
        GridView1.DataSource = myDataSet;
        GridView1.DataBind();
        viewstate.add("myDataSet",myDataSet);

    //close the sql connection
    mySQLconnection.Close();
}

在页面索引更改事件中,执行此操作... protected void GridView1_PageIndexChanging(对象发送者,GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView.datasource=(DataTable)ViewState["myDataSet"); GridView.DataBind(); }

如果 viewstate 给出了一些关于序列化的错误,请使用 session。这将使您的代码更加高效和优化。

【讨论】:

  • 谢谢你,我知道这会如何让事情变得更有效率 - 我会努力工作:)
猜你喜欢
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
相关资源
最近更新 更多