C#自定义表分页问题

时间:2017-06-02 17:33:36

标签: c# asp.net

我创建了一个asp.net webapplication,它在我的MySQL数据库中显示html表格中的数据,我这样做是为了一次只能看到20行,而在button_click事件中可以看到接下来的20行,并且等等。但是,在第6次点击后,它会显示40行而不能再点击该按钮。

HTML

<div>
    <table id="datagrid" runat="server">
    </table>
    <asp:Button OnClick="Unnamed_Click" runat="server"/>
</div>

C#代码[最终 - 工作,任何人都需要它]

int minrows = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        DisplayData();
    }
    private void DisplayData()
    {
        if (ViewState["minrows"] != null)
        {
            minrows = Convert.ToInt16(ViewState["minrows"]);
        }
        MySqlConnection conn = new MySqlConnection("server=localhost;database=apollo;user=root;password=mysqltest");
        MySqlCommand cmd = new MySqlCommand("SELECT Klantnr, Naam, Straat FROM tblklanten LIMIT "+minrows+", 20", conn);
        conn.Open();
        MySqlDataReader Reader = cmd.ExecuteReader();
        while (Reader.Read())
        {
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td1 = new HtmlTableCell();
            HtmlTableCell td2 = new HtmlTableCell();
            HtmlTableCell td3 = new HtmlTableCell();
            datagrid.Controls.Add(tr);
            td1.InnerText = Reader["Klantnr"].ToString();
            td2.InnerText = Reader["Naam"].ToString();
            td3.InnerText = Reader["Straat"].ToString();
            tr.Controls.Add(td1);
            tr.Controls.Add(td2);
            tr.Controls.Add(td3);
        }
        Reader.Close();
        conn.Close();
    }
    protected void Unnamed_Click(object sender, EventArgs e)
    {
        minrows += 20;
        ViewState["minrows"] = minrows;
        datagrid.Controls.Clear();
        DisplayData();
    }

1 个答案:

答案 0 :(得分:1)

您错误地使用MySQL.LIMIT子句

https://www.w3schools.com/php/php_mysql_select_limit.asp

"... LIMIT " + minrows + ", " + (maxrows - minrows)