cs:protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
NewsBind();
}
}
private void NewsBind()//repeater分页并绑定
{
string SqlStr;
string areacode ;
string range;
if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
{
areacode = Request.QueryString["areacode"].ToString();
range = "";
SqlStr = "select company.companyno,company.address,company.phone,company.id,company.description,companycatalog.nodename,member.nodecode from company,companycatalog,member where company.nodecode=companycatalog.nodecode and company.id=member.companyid and company.nodecode='" + @areacode + "' order by member.nodecode desc,company.id desc";
lbl_something.Text = "企业大全";
}
else if (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
{
range= Request.QueryString["type"].ToString();
areacode = "";
SqlStr = "select company.companyno,company.address,company.phone,company.id,company.description,companycatalog.nodename,member.nodecode from company,companycatalog,member where company.nodecode=companycatalog.nodecode and company.id=member.companyid and company.res like '%" + @range + "%' order by member.nodecode desc,company.id desc";
lbl_something.Text = range + "类企业大全";
}
else{
range = "";
areacode = "";
SqlStr = "select company.companyno,company.address,company.phone,company.id,company.description,companycatalog.nodename,member.nodecode from company,companycatalog,member where company.nodecode=companycatalog.nodecode and company.id=member.companyid order by member.nodecode desc,company.id desc";
lbl_something.Text = "企业大全";
}
string strconn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter(SqlStr, conn);
DataSet ds = new DataSet();
try
{
Adapter.Fill(ds, "testTable"); PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = ds.Tables["testTable"].DefaultView;
objPage.AllowPaging = true;
objPage.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
Repeater1.DataSource = objPage;//这里更改控件名称
Repeater1.DataBind();//这里更改控件名称
RecordCount.Text = objPage.DataSourceCount.ToString();
PageCount.Text = objPage.PageCount.ToString();
Pageindex.Text = CurPage.ToString();
Literal1.Text = PageList(objPage.PageCount, CurPage);
//Literal1.Text = PageList(objPage.PageCount, Pageindex, L_Manage); //带参数的:LManage为参数
FirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1&areacode="+areacode+"&type="+range;
PrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage - 1) + "&areacode=" + areacode + "&type=" + range;
NextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage + 1) + "&areacode=" + areacode + "&type=" + range;
LastPaeg.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + objPage.PageCount.ToString() + "&areacode=" + areacode + "&type=" + range;
if (CurPage <= 1 && objPage.PageCount <= 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/**/
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
if (CurPage <= 1 && objPage.PageCount > 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
/**/
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
*/
}
if (CurPage >= objPage.PageCount)
{
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/**/
/*
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
//主要是这一段,要显示每个公司的评论数量
for (int i = 0; i < Repeater1.Items.Count; i++)
{
string strcn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn = new SqlConnection(strcn);
cn.Open();
int companyid = Convert.ToInt32(ds.Tables["testTable"].Rows[i]["id"].ToString());
string strsql = "select count(id) from pinglun where companyid=" + companyid + "";
SqlCommand cm = new SqlCommand(strsql, cn); SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
Label tmp = (Label)Repeater1.Items[i].FindControl("lbl_ppnum");
tmp.Text = dr[0].ToString();
}
} }
catch (Exception error)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
private string PageList(int Pagecount, int Pageindex)//private string Jump_List(int Pagecount , int Pageindex , long L_Manage)//带参数的传递
{
string areacode;
string range; if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
{
areacode = Request.QueryString["areacode"].ToString();
range = "";
}
else if (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
{
range = Request.QueryString["type"].ToString();
areacode = "";
} else
{
range = "";
areacode = "";
}
StringBuilder sb = new StringBuilder();
//下为带参数的传递
//sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&type=" + L_Manage + "';\">");
//不带参数的传递
sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&type=" + range + "'+'&areacode=" + areacode+ "';\">"); for (int i = 1; i <= Pagecount; i++)
{
if (Pageindex == i)
sb.Append("<option value='" + i + "' selected>" + i + "</option>");
else
sb.Append("<option value='" + i + "'>" + i + "</option>");
}
sb.Append("</select>");
return sb.ToString();
}结果只有第一页显示正常,后面几页显示的数量都是第一页的,该怎么整啊
{
if (!Page.IsPostBack)
{
NewsBind();
}
}
private void NewsBind()//repeater分页并绑定
{
string SqlStr;
string areacode ;
string range;
if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
{
areacode = Request.QueryString["areacode"].ToString();
range = "";
SqlStr = "select company.companyno,company.address,company.phone,company.id,company.description,companycatalog.nodename,member.nodecode from company,companycatalog,member where company.nodecode=companycatalog.nodecode and company.id=member.companyid and company.nodecode='" + @areacode + "' order by member.nodecode desc,company.id desc";
lbl_something.Text = "企业大全";
}
else if (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
{
range= Request.QueryString["type"].ToString();
areacode = "";
SqlStr = "select company.companyno,company.address,company.phone,company.id,company.description,companycatalog.nodename,member.nodecode from company,companycatalog,member where company.nodecode=companycatalog.nodecode and company.id=member.companyid and company.res like '%" + @range + "%' order by member.nodecode desc,company.id desc";
lbl_something.Text = range + "类企业大全";
}
else{
range = "";
areacode = "";
SqlStr = "select company.companyno,company.address,company.phone,company.id,company.description,companycatalog.nodename,member.nodecode from company,companycatalog,member where company.nodecode=companycatalog.nodecode and company.id=member.companyid order by member.nodecode desc,company.id desc";
lbl_something.Text = "企业大全";
}
string strconn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter(SqlStr, conn);
DataSet ds = new DataSet();
try
{
Adapter.Fill(ds, "testTable"); PagedDataSource objPage = new PagedDataSource();
objPage.DataSource = ds.Tables["testTable"].DefaultView;
objPage.AllowPaging = true;
objPage.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
Repeater1.DataSource = objPage;//这里更改控件名称
Repeater1.DataBind();//这里更改控件名称
RecordCount.Text = objPage.DataSourceCount.ToString();
PageCount.Text = objPage.PageCount.ToString();
Pageindex.Text = CurPage.ToString();
Literal1.Text = PageList(objPage.PageCount, CurPage);
//Literal1.Text = PageList(objPage.PageCount, Pageindex, L_Manage); //带参数的:LManage为参数
FirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1&areacode="+areacode+"&type="+range;
PrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage - 1) + "&areacode=" + areacode + "&type=" + range;
NextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage + 1) + "&areacode=" + areacode + "&type=" + range;
LastPaeg.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + objPage.PageCount.ToString() + "&areacode=" + areacode + "&type=" + range;
if (CurPage <= 1 && objPage.PageCount <= 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/**/
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
if (CurPage <= 1 && objPage.PageCount > 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
/**/
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
*/
}
if (CurPage >= objPage.PageCount)
{
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/**/
/*
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
//主要是这一段,要显示每个公司的评论数量
for (int i = 0; i < Repeater1.Items.Count; i++)
{
string strcn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn = new SqlConnection(strcn);
cn.Open();
int companyid = Convert.ToInt32(ds.Tables["testTable"].Rows[i]["id"].ToString());
string strsql = "select count(id) from pinglun where companyid=" + companyid + "";
SqlCommand cm = new SqlCommand(strsql, cn); SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
Label tmp = (Label)Repeater1.Items[i].FindControl("lbl_ppnum");
tmp.Text = dr[0].ToString();
}
} }
catch (Exception error)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
private string PageList(int Pagecount, int Pageindex)//private string Jump_List(int Pagecount , int Pageindex , long L_Manage)//带参数的传递
{
string areacode;
string range; if (Request.QueryString["areacode"] != null && Request.QueryString["areacode"] != "")
{
areacode = Request.QueryString["areacode"].ToString();
range = "";
}
else if (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
{
range = Request.QueryString["type"].ToString();
areacode = "";
} else
{
range = "";
areacode = "";
}
StringBuilder sb = new StringBuilder();
//下为带参数的传递
//sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&type=" + L_Manage + "';\">");
//不带参数的传递
sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&type=" + range + "'+'&areacode=" + areacode+ "';\">"); for (int i = 1; i <= Pagecount; i++)
{
if (Pageindex == i)
sb.Append("<option value='" + i + "' selected>" + i + "</option>");
else
sb.Append("<option value='" + i + "'>" + i + "</option>");
}
sb.Append("</select>");
return sb.ToString();
}结果只有第一页显示正常,后面几页显示的数量都是第一页的,该怎么整啊
for (int i = 0; i < Repeater1.Items.Count; i++)
{
string strcn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn = new SqlConnection(strcn);
cn.Open();
int companyid = Convert.ToInt32(ds.Tables["testTable"].Rows[i]["id"].ToString());
string strsql = "select count(id) from pinglun where companyid=" + companyid + "";
SqlCommand cm = new SqlCommand(strsql, cn); SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
Label tmp = (Label)Repeater1.Items[i].FindControl("lbl_ppnum");
tmp.Text = dr[0].ToString();
}
}
结果只有第一页显示正常,后面几页显示的数量都是第一页的,该怎么整啊
算法逻辑什么的我不太明白,我已经把后台代码全部帖出来了,前台代码如下
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table width="660" border="0" cellpadding="4" cellspacing="0" bgcolor='#FCFDEA'>
<tr align="left">
<td ><a href="detail.aspx?id=<%#Convert.ToString(Eval("id"))%>" style="font-size:16px; " title=<%#Convert.ToString(Eval("companyno"))%>><b><%#Convert.ToString(Eval("companyno"))%></b></a></td>
</tr>
<tr align="left">
<td class="black-12">
<asp:Label ID="lbl_ppnum" runat="server" ></asp:Label>条客户点评 </td>
</tr>
</table>
</ItemTemplate> </asp:Repeater>
<table border="0" width=660 style="text-align:center; background-color:Lime"><tr><td align="center">
共有<asp:Literal ID="RecordCount" runat="server"></asp:Literal>条记录
共有<asp:Literal ID="PageCount" runat="server"></asp:Literal>页
当前第<asp:Literal ID="Pageindex" runat="server"></asp:Literal>页
<asp:HyperLink ID="FirstPage" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="PrevPage" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="NextPage" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="LastPaeg" runat="server" Text="尾页"></asp:HyperLink>
跳转到<asp:Literal ID="Literal1" runat="server"></asp:Literal>页
</td></tr>
</table>