我是初学者并尝试使用c#在asp.net中的SQL Server数据库表中显示下一个/上一个记录。但它不起作用......
当我按下下一个按钮时,只有一次它起作用,下次我按下它不起作用。我试图找出问题所在,我发现变量rowIndex
在按下下一个按钮一次后没有递增。
并且不知道前一个按钮是否有效,因为下一个按钮只能工作一次,前一个按钮也可以工作一次。
我不知道如何解决这个问题因为我是初学者,所以请帮帮我:)。
这是Home.aspx.cs
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Home : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt = new DataTable();
int rowIndex;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = DashingGoal; Integrated Security = True; Connect Timeout = 30; Encrypt = False;
TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False");
con.Open();
cmd = new SqlCommand("select * from content", con);
da = new SqlDataAdapter(cmd);
da.Fill(dt);
rowIndex = 0;
Label2.Text = dt.Rows.Count.ToString();
if (!Page.IsPostBack)
{
if (dt.Rows.Count > 0)
{
// Populate the TextBox with the first entry on page load
Label1.Text = dt.Rows[0]["heading"].ToString();
//Then we store the DataTable in Session so that we will NOT
//query the DB on every postbacks
Session["dt"] = dt;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Session["dt"] != null)
{
dt = (DataTable)Session["dt"];
if (rowIndex < dt.Rows.Count)
{
rowIndex++;
Label3.Text = rowIndex.ToString();
//get the next row entry on Button Click by setting the Row Index
Label1.Text = dt.Rows[rowIndex]["heading"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (rowIndex == ds.Tables[0].Rows.Count - 1 || rowIndex != 0)
{
rowIndex--;
Label1.Text = ds.Tables[0].Rows[rowIndex]["heading"].ToString();
}
}
}
这是Home.aspx
页面的标记:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#form1 {
height: 809px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Font-Bold="False" Font-Size="XX-Large" OnClick="Button1_Click" style="z-index: 1; left: 51px; top: 261px; position: absolute; width: 57px; height: 41px" Text="<" />
<asp:Button ID="Button2" runat="server" Font-Size="XX-Large" OnClick="Button2_Click" style="z-index: 1; left: 860px; top: 262px; position: absolute; height: 41px; width: 57px" Text=">" />
<asp:Label ID="Label1" runat="server" style="z-index: 1; left: 111px; top: 444px; position: absolute; height: 61px; width: 744px"></asp:Label>
<asp:Label ID="Label2" runat="server" style="z-index: 1; left: 91px; top: 34px; position: absolute; height: 27px; width: 68px" Text="Label"></asp:Label>
<asp:Label ID="Label3" runat="server" style="z-index: 1; left: 225px; top: 27px; position: absolute; height: 22px; width: 51px" Text="Label"></asp:Label>
</form>
</body>
</html>
答案 0 :(得分:0)
是的,它不应该那么好用。再次检查您的代码(如下所示)。 if (rowIndex == ds.Tables[0].Rows.Count - 1
你要去哪个rowindex吗?这是完全错误的;在按钮点击事件中,您没有获得rowindex。您应该使用启用了GridView
的{{1}}控件,并将网格中的记录设置为1或您想要的任何数字。
Paging