repeater控件与AspNetPager结合 AspNetPager不显示
代码如下,求大神们帮助啊!!!
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using Wuqi.Webdiyer;
namespace MeApp
{
public partial class weathedit : System.Web.UI.Page
{
int id = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
//bindData(); //使用url分页,只需在分页事件处理程序中绑定数据即可,无需在Page_Load中绑定,否则会导致数据被绑定两次
}
}
private void BindGrid()
{
string settings = ConfigurationManager.AppSettings["ConStr"].ToString();
//Response.Write(settings);
//Response.End();
SqlConnection sqlCon = new SqlConnection(settings);
sqlCon.Open(); //打开数据库连接
string sqlstr = "select * from WeatherInfo";
SqlCommand mycmd = new SqlCommand(sqlstr, sqlCon);//创建数据库命令
SqlDataReader mydr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
//Repeater数据控件绑定
rptUser.DataSource = mydr;//指定数据源
rptUser.DataBind();//绑定到指定的数据源
}
protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;
int userId = int.Parse(record["weinfo_id"].ToString());
if (userId != id)
{
((Panel)e.Item.FindControl("plItem")).Visible = true;
((Panel)e.Item.FindControl("plEdit")).Visible = false;
}
else
{
((Panel)e.Item.FindControl("plItem")).Visible = false;
((Panel)e.Item.FindControl("plEdit")).Visible = true;
}
}
}
protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
id = int.Parse(e.CommandArgument.ToString());
}
else if (e.CommandName == "Cancel")
{
id = -1;
}
else if (e.CommandName == "Update")
{
string weatherdate = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtdate")).Text.Trim();
string weathertime = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txttime")).Text.Trim();
string weath = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtweather")).Text.Trim();
string tem = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txttem")).Text.Trim();
string hum = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txthum")).Text.Trim();
string airQ = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtair")).Text.Trim();
string strsql = "UPDATE [WeatherInfo] SET weinfo_date=@weinfo_date,weather_time=@weather_time,weather=@weather,temperature=@temperature,huminity=@huminity,airQuality=@airQuality WHERE weinfo_id=@weinfo_id";
string settings = ConfigurationManager.AppSettings["ConStr"].ToString();
SqlConnection sqlCon = new SqlConnection(settings);
SqlCommand sqlcom = new SqlCommand(strsql, sqlCon);
sqlcom.Parameters.Add("@weinfo_date", SqlDbType.DateTime);
sqlcom.Parameters["@weinfo_date"].Value = weatherdate;
sqlcom.Parameters.Add("@weather_time", SqlDbType.VarChar);
sqlcom.Parameters["@weather_time"].Value = weathertime;
sqlcom.Parameters.Add("@weather", SqlDbType.VarChar);
sqlcom.Parameters["@weather"].Value = weath;
sqlcom.Parameters.Add("@temperature", SqlDbType.VarChar);
sqlcom.Parameters["@temperature"].Value = tem;
sqlcom.Parameters.Add("@huminity", SqlDbType.VarChar);
sqlcom.Parameters["@huminity"].Value = hum;
sqlcom.Parameters.Add("@airQuality", SqlDbType.VarChar);
sqlcom.Parameters["@airQuality"].Value = airQ;
sqlcom.Parameters.Add("@weinfo_id", SqlDbType.Int);
sqlcom.Parameters["@weinfo_id"].Value = int.Parse(e.CommandArgument.ToString());
sqlCon.Open();
sqlcom.ExecuteNonQuery();
sqlCon.Close();
}
else if (e.CommandName == "Delete")
{
string strSQL = "DELETE * FROM [WeatherInfo] WHERE weinfo_id=@weinfo_id";
string settings = ConfigurationManager.AppSettings["ConStr"].ToString();
SqlConnection sqlCon = new SqlConnection(settings);
SqlCommand sqlcom = new SqlCommand(strSQL, sqlCon);
sqlcom.Parameters.Add("@weinfo_id", SqlDbType.Int);
sqlcom.Parameters["@weinfo_id"].Value = int.Parse(e.CommandArgument.ToString());
sqlCon.Open();
sqlcom.ExecuteNonQuery();
sqlCon.Close();
}
BindGrid();
}
public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
string settings = ConfigurationManager.AppSettings["ConStr"].ToString();
SqlConnection sqlCon = new SqlConnection(settings);
SqlDataAdapter ada = new SqlDataAdapter(sql,sqlCon);
DataSet ds = new DataSet();
int startRow = (currentPage - 1) * pagesize;
ada.Fill(ds, startRow, pagesize, "table");
recordcount = GetPageRecord(sql);
return ds;
}
public int GetPageRecord(string sql)
{
string settings = ConfigurationManager.AppSettings["ConStr"].ToString();
SqlConnection sqlCon = new SqlConnection(settings);
sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
return recordcount;
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGrid();
}
}
}
[解决办法]
没有为AspNetPager设置RecordCount的值,请参考一下示例和帮助文档:http://www.webdiyer.com/Controls/AspNetPager