《做收藏遇到问题》由会员分享,可在线阅读,更多相关《做收藏遇到问题(14页珍藏版)》请在金锄头文库上搜索。
1、 收藏功能问题收藏的 ALLItems 页面用了 Datalist 做;因此没有系统自带分页,只能自己写分页功能;#region 这是收藏后台代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Web.Common; using System.Data; using System.Data.SqlClient; using System.Configur
2、ation; namespace Web.Collections.UserControl public partial class WUCGrid : BasedUserControlprotected void Page_Load(object sender, EventArgs e)if (!IsPostBack)ViewState“pageindex“ = 0;BindData();private int _ArticleID;public int ArticleIDget return _ArticleID; set _ArticleID=value; protected void l
3、nkBtnTitle_Click(object sender, EventArgs e)LinkButton lnkBtnTitle = (LinkButton)sender;if (lnkBtnTitle != null)ArticleID = Convert.ToInt32( lnkBtnTitle.CommandArgument);/查询对应的 ArticleID 的 ispublic 是否为 1;为公开;eSchool.JXKYW.Library.Articles articles = (from p in DataContext.Articleswhere p.ID.Equals(A
4、rticleID) if (articles=null)throw new Exception(“文章已被删除!“);protected void dl_DataBinding(object sender, EventArgs e)/删除事件protected void dl_DeleteCommand(object source, DataListCommandEventArgs e)int Key = Convert.ToInt32(dl.DataKeyse.Item.ItemIndex.ToString();/获取激发 事件时的行的主键 IDString conString = Conf
5、igurationManager.ConnectionStrings“JXKYWConnectionString“.ConnectionString;String strSql = “delete from Collections where ID=“ + Key + “;int rows = ExecuteSql(strSql, conString);BindData();public int ExecuteSql(string strSql,string conString)using (SqlConnection myConnection=new SqlConnection(conStr
6、ing)using (SqlCommand cmd=new SqlCommand(strSql, myConnection)myConnection.Open();int rows=cmd.ExecuteNonQuery();return rows;#region DataList 分页public static DataSet GetTable(string sql) SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings “JXKYWConnectionString“.ConnectionS
7、tring); SqlDataAdapter da = new SqlDataAdapter(sql, conn);DataSet ds = new DataSet(); da.Fill(ds);return ds;public void BindData()string LoginName = this.CurrentLoginName;string sql = “select * from vw_CollectionsArticles where CollectorName=“+LoginName+“;DataSet objds = GetTable(sql);PagedDataSourc
8、e objPds = new PagedDataSource();objPds.DataSource = objds.Tables0.DefaultView;objPds.AllowPaging = true;objPds.PageSize = 10;objPds.CurrentPageIndex = int.Parse(ViewState“pageindex“.ToString();if (!objPds.IsFirstPage)lkPre.Visible = true;elselkPre.Visible = false;if (!objPds.IsLastPage)lkNext.Visib
9、le = true;elselkNext.Visible = false;/如果没有找到数据就隐藏 pnlNoArticleif (objPds.DataSourceCount 0)this.FindControl(“pnlNoArticle“).Visible = false;elsethis.FindControl(“pnlNoArticle“).Visible = true;dl.DataSource = objPds;dl.DataKeyField = “ID“;dl.DataBind();/*上一页下一页的事件;I 两个按钮的 OnCommand 都是 IndexChanging()
10、函数, 我们通过 CommandArgument(pre 和 next)来区分到底是向前翻还是向后翻。下面是 IndexChanging()函数*/protected void IndexChanging(object sender, EventArgs e)string strCommand = (LinkButton)sender).CommandArgument.ToString();/得到 linkbtn 的 CommandArgument 值int pageindex = int.Parse(ViewState“pageindex“.ToString();if (strCommand
11、 = “pre“)pageindex = pageindex - 1;elsepageindex = pageindex + 1;ViewState“pageindex“ = pageindex;BindData();#endregionprivate string _AuthorName;protected string AuthorNameget return _AuthorName; set _AuthorName = value; protected void hylArticleAuthor_Click(object sender, EventArgs e)LinkButton hy
12、lArticleAuthor=(LinkButton)sender;if(hylArticleAuthor!=null)AuthorName=hylArticleAuthor.Text.ToString();eSchool.JXKYW.Library.UserMappings UserMapping = (from p in DataContext.UserMappingswhere p.LoginName.Equals(AuthorName)select p).FirstOrDefault();if(UserMapping!=null)AnonymouEntityID = Convert.T
13、oInt32(UserMapping.EntityID);AnonymouEntityType = “Contact“;Response.Redirect(“/PersonalDefault.aspx?Type=3“); #endregion#region 这是前台代码上一页 下一页收藏日期: /:标题: 25?(Eval(“ArticleTitle“).ToString().Substring(0,25):Eval(“ArticleTitle“) %OnClick=“lnkBtnTitle_Click“ CommandArgument=内容:35?(Eval(“ArticleContent“
14、).ToString().Substring(0,35)+“.“:Eval(“ArticleContent“) %来自: 删除噢NO您还没有收藏过文章,赶快去收藏吧 #endregion一。以下是分页功能: #region DataList 分页public static DataSet GetDataSet(string sql)/得到数据集 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings “JXKYWConnectionString“.ConnectionString); SqlDa
15、taAdapter da = new SqlDataAdapter(sql, conn);DataSet ds = new DataSet(); da.Fill(ds);return ds;public void BindData()string LoginName = this.CurrentLoginName;/得到当前登录名;string sql = “select * from vw_CollectionsArticles where CollectorName=“+LoginName+“;DataSet objds = GetDataSet(sql);PagedDataSource objPds = new PagedDataSource();objPds.DataSource = objds.Tables0.DefaultView;/得到数据表;数据集的 第一个 Table;为默认数据源objPds.AllowPaging = true;/允许分页objPds.PageSize = 10;objPds.CurrentPageIndex = int.Parse(ViewState“pageindex“.ToString();/*CurrentPageIndex 获取或设置当前页的索引。用视图来保存 pageindex;没有视图时候给 0;在 page