我在这行代码中遇到错误:
CurrentPage = FillRepeater() – 1;
对于分页目的,我使用PagedDataSource
类,它与在其他数据绑定控件中用于分页的类相同。在FillRepeater()
函数中,我创建了PagedDataSource
类的对象并使用了它的一些方法。这个简单的属性用于获取当前页面值如下。这很简单:
viewstate
存储数据页面的当前状态以下是代码:
public partial class ExamPage : System.Web.UI.Page
{
SqlConnection con;
string query;
public ExamPage()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private int FillRepeater()
{
query = "select top 10 Question,Option1,Option2,Option3,Option4 from Questions";
SqlCommand cmd = new SqlCommand(query, con);
con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true; pds.PageSize = 8;
int count = pds.PageCount;
pds.CurrentPageIndex = CurrentPage;
if (pds.Count > 0)
{
lbtnPrev.Visible = true;
lbtnNext.Visible = true;
lbtnFirst.Visible = true;
lbtnLast.Visible = true;
lblStatus.Text = "Page " + Convert.ToString(CurrentPage + 1) + "of" + Convert.ToString(pds.PageCount);
}
else {
lbtnPrev.Visible = false;
lbtnNext.Visible = false;
lbtnFirst.Visible = false;
lbtnLast.Visible = false;
}
lbtnPrev.Enabled = !pds.IsFirstPage;
lbtnNext.Enabled = !pds.IsLastPage;
lbtnFirst.Enabled = !pds.IsFirstPage;
lbtnLast.Enabled = !pds.IsLastPage;
Repeater1.DataSource = pds;
Repeater1.DataBind();
return count;
}
public int CurrentPage
{
get
{
object obj = this.ViewState["_CurrentPage"]; if (obj == null) { return 0; }
else
{
return (int)obj;
}
}
set
{
//set in viewstate the current page number
this.ViewState["_CurrentPage"] = value;
}
}
protected void lbtnPrev_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
FillRepeater();
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
FillRepeater();
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
FillRepeater();
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = FillRepeater() – 1;
FillRepeater();
}
}
我收到语法错误而不是运行时错误,但我不知道如何解决它。请告诉我我在哪里犯错误
答案 0 :(得分:0)
CurrentPage = FillRepeater() – 1;
在该行中,–
字符为en dash。大多数编程语言只允许您使用hyphen-minus,-
来表示减法。连字符 - 减号键出现在大多数关键字上。
CurrentPage = FillRepeater() - 1;
您在代码中使用短划线的原因是它可能是使用文字处理软件(例如Microsoft Word)创作的,当下一个单词后跟一个文字处理软件时,它会自动将hypen-minus转换为短划线。空间。