做收藏遇到问题.docx
- 文档编号:7079256
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:19
- 大小:20.64KB
做收藏遇到问题.docx
《做收藏遇到问题.docx》由会员分享,可在线阅读,更多相关《做收藏遇到问题.docx(19页珍藏版)》请在冰豆网上搜索。
做收藏遇到问题
收藏功能问题
收藏的ALLItems页面用了Datalist做;因此没有系统自带分页,只能自己写分页功能;
#region这是收藏后台代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingWeb.Common;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
namespaceWeb.Collections.UserControl
{
publicpartialclassWUCGrid:
BasedUserControl
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!
IsPostBack)
{
ViewState["pageindex"]=0;
BindData();
}
}
privateint_ArticleID;
publicintArticleID
{
get{return_ArticleID;}
set{_ArticleID=value;}
}
protectedvoidlnkBtnTitle_Click(objectsender,EventArgse)
{
LinkButtonlnkBtnTitle=(LinkButton)sender;
if(lnkBtnTitle!
=null)
{
ArticleID=Convert.ToInt32(lnkBtnTitle.CommandArgument);
}
//查询对应的ArticleID的ispublic是否为1;为公开;
eSchool.JXKYW.Library.Articlesarticles=(frompinDataContext.Articles
wherep.ID.Equals(ArticleID)&&p.IsPublic.Equals
(1)&&p.UsedFlag.Equals
(1)
selectp).FirstOrDefault();
if(articles==null)
{
thrownewException("文章已被删除!
");
}
}
protectedvoiddl_DataBinding(objectsender,EventArgse)
{
}
//删除事件
protectedvoiddl_DeleteCommand(objectsource,DataListCommandEventArgse)
{
intKey=Convert.ToInt32(dl.DataKeys[e.Item.ItemIndex].ToString());//获取激发事件时的行的主键ID
StringconString=ConfigurationManager.ConnectionStrings["JXKYWConnectionString"].ConnectionString;
StringstrSql="deletefromCollectionswhereID='"+Key+"'";
introws=ExecuteSql(strSql,conString);
BindData();
}
publicintExecuteSql(stringstrSql,stringconString)
{
using(SqlConnectionmyConnection=newSqlConnection(conString))
{
using(SqlCommandcmd=newSqlCommand(strSql,myConnection))
{
myConnection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
}
}
#regionDataList分页
publicstaticDataSetGetTable(stringsql)
{
SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings
["JXKYWConnectionString"].ConnectionString);
SqlDataAdapterda=newSqlDataAdapter(sql,conn);
DataSetds=newDataSet();
da.Fill(ds);
returnds;
}
publicvoidBindData()
{
stringLoginName=this.CurrentLoginName;
stringsql="select*fromvw_CollectionsArticleswhereCollectorName='"+LoginName+"'";
DataSetobjds=GetTable(sql);
PagedDataSourceobjPds=newPagedDataSource();
objPds.DataSource=objds.Tables[0].DefaultView;
objPds.AllowPaging=true;
objPds.PageSize=10;
objPds.CurrentPageIndex=int.Parse(ViewState["pageindex"].ToString());
if(!
objPds.IsFirstPage)
{
lkPre.Visible=true;
}
else
{
lkPre.Visible=false;
}
if(!
objPds.IsLastPage)
{
lkNext.Visible=true;
}
else
{
lkNext.Visible=false;
}
//如果没有找到数据就隐藏pnlNoArticle
if(objPds.DataSourceCount>0)
{
this.FindControl("pnlNoArticle").Visible=false;
}
else
{
this.FindControl("pnlNoArticle").Visible=true;
}
dl.DataSource=objPds;
dl.DataKeyField="ID";
dl.DataBind();
}
/*上一页下一页的事件;I两个按钮的OnCommand都是IndexChanging()函数,我们通过CommandArgument(pre和next)来区分到底是向前翻还是向后翻。
下面是IndexChanging()函数*/
protectedvoidIndexChanging(objectsender,EventArgse)
{
stringstrCommand=((LinkButton)sender).CommandArgument.ToString();//得到linkbtn的CommandArgument值
intpageindex=int.Parse(ViewState["pageindex"].ToString());
if(strCommand=="pre")
{
pageindex=pageindex-1;
}
else
{
pageindex=pageindex+1;
}
ViewState["pageindex"]=pageindex;
BindData();
}
#endregion
privatestring_AuthorName;
protectedstringAuthorName
{
get{return_AuthorName;}
set{_AuthorName=value;}
}
protectedvoidhylArticleAuthor_Click(objectsender,EventArgse)
{
LinkButtonhylArticleAuthor=(LinkButton)sender;
if(hylArticleAuthor!
=null)
{
AuthorName=hylArticleAuthor.Text.ToString();
}
eSchool.JXKYW.Library.UserMappingsUserMapping=(frompinDataContext.UserMappings
wherep.LoginName.Equals(AuthorName)
selectp).FirstOrDefault();
if(UserMapping!
=null)
{
AnonymouEntityID=Convert.ToInt32(UserMapping.EntityID);
AnonymouEntityType="Contact";
Response.Redirect("~/PersonalDefault.aspx?
Type=3");
}
}
}
}
#endregion
#region这是前台代码
<%@ControlLanguage="C#"AutoEventWireup="true"CodeBehind="WUCGrid.ascx.cs"Inherits="Web.Collections.UserControl.WUCGrid"%>
<%@Registersrc="../../Common/UserControl/ToolBar.ascx"tagname="ToolBar"tagprefix="uc1"%>
<%@Registersrc="../../Common/UserControl/Pager.ascx"tagname="Pager"tagprefix="uc2"%>
<%@Registersrc="../../Common/UserControl/SecondLevelMenu.ascx"tagname="SecondLevelMenu"tagprefix="uc3"%>
SecondLevelMenuID="SecondLevelMenu1"runat="server"/> LabelID="Label1"runat="server"Text="没变化"> Label> LinkButtonID="lkPre"runat="server"CommandArgument="pre"OnCommand="IndexChanging">上一页 LinkButton> linkbuttonID="lkNext"OnCommand="IndexChanging"CommandArgument="next"runat="server">下一页 linkbutton> DataListID="dl"runat="server"DataKeyField="ID" ondatabinding="dl_DataBinding"ondeletecommand="dl_DeleteCommand"> 收藏日期: LabelID="IDLabel"runat="server"Text='<%#Eval("CreateTime","{0: yyyy-mm-dd}")%>'/> LabelID="lblCollName"runat="server"><%#Eval("CollectorName")%>: <%#Eval("Remark")%> Label> 标题: LinkButtonID="lnkBtnTitle"runat="server"Text='<%#(Eval("ArticleTitle")).ToString().Length>25? (Eval("ArticleTitle")).ToString().Substring(0,25): Eval("ArticleTitle")%>' OnClick="lnkBtnTitle_Click"CommandArgument='<%#Eval("ArticleID")%>'> LinkButton> Gray"> 内容: <%#(Eval("ArticleContent")).ToString().Length>35? (Eval("ArticleContent")).ToString().Substring(0,35)+"......": Eval("ArticleContent")%> 来自: LinkButtonID="hylArticleAuthor"runat="server"OnClick="hylArticleAuthor_Click"Text='<%#Eval("AuthorName")%>'> LinkButton> LinkButtonID="lnkBtnDelete"CommandName="Delete"OnClientClick="returnconfirm('是否确认删除? ');"runat="server">删除 LinkButton> DataList> --如果没有数据就显示--> PanelID="pnlNoArticle"runat="server"> @华文楷体;font-size: xx-large;color: Gray"> LabelID="lblNo"runat="server">噢~~~~NO~~~~您还没有收藏过文章,赶快去收藏吧~ Label>
Panel>
#endregion
一。
以下是分页功能:
#regionDataList分页
publicstaticDataSetGetDataSet(stringsql)//得到数据集
{
SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings
["JXKYWConnectionString"].ConnectionString);
SqlDataAdapterda=newSqlDataAdapter(sql,conn);
DataSetds=newDataSet();
da.Fill(ds);
returnds;
}
publicvoidBindData()
{
stringLoginName=this.CurrentLoginName;//得到当前登录名;
stringsql="select*fromvw_CollectionsArticleswhereCollectorName='"+LoginName+"'";
DataSetobjds=GetDataSet(sql);
PagedDataSourceobjPds=newPagedDataSource();
objPds.DataSource=objds.Tables[0].DefaultView;//得到数据表;数据集的第一个Table;为默认数据源
objPds.AllowPaging=true;//允许分页
objPds.PageSize=10;
objPds.CurrentPageIndex=int.Parse(ViewState["pageindex"].ToString());
/*CurrentPageIndex获取或设置当前页的索引。
用视图来保存pageindex;
没有视图时候给0;在page_load事件中给值为0
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!
IsPostBack)
{
ViewState["pageindex"]=0;
BindData();这里就执行一下分页函数
}
}*/
if(!
objPds.IsFirstPage)//IsFirstPage获取一个值,该值指示当前页是否是首页。
{
lkPre.Visible=true;
}
else
{
lkPre.Visible=false;
}
if(!
objPds.IsLastPage)
{
lkNext.Visible=true;
}
else
{
lkNext.Visible=false;
}
//如果没有找到数据就隐藏pnlNoArticle
if(objPds.DataSourceCount>0)//DataSourceCount获取数据源中的项数。
{
this.FindControl("pnlNoArticle").Visible=false;
}
else
{
this.FindControl("pnlNoArticle").Visible=true;
}
dl.DataSource=objPds;//将datalist的数据源设置为pagedatasourse
dl.DataKeyField="ID";//关键值
dl.DataBind();//然后最后在讲datalist绑定一下;
}
/*上一页下一页的事件;I两个按钮的OnCommand都是IndexChanging()函数,我们通过CommandArgument(pre和next)来区分到底是向前翻还是向后翻。
下面是IndexChanging()函数*/
protectedvoidIndexChanging(objectsender,EventArgse)
{
stringstrCommand=((LinkButton)sender).CommandArgument.ToString();//得到linkbtn的CommandArgument值
intpageindex=int.Parse(ViewState["pageindex"].ToString());
if(strCommand=="pre")
{
pageindex=pageindex-1;
}
else
{
pageindex=pageindex+1;
}
ViewState["pageindex"]=pageindex;
BindData();//这里很重要;要再一次执行分页函数;
}
#endregion
二。
查找数据库中某张表中的某条数据是否存在
#region
eSchool.JXKYW.Library.Articlesarticles=(frompinDataContext.Articles
wherep.ID.Equals(ArticleID)&&p.IsPublic.Equals
(1)&&p.UsedFlag.Equals
(1)
selectp).FirstOrDefault();
#endregion
三。
弹框问题;
ScriptManager.RegisterStartupScript(this.Page,this.Page.GetType(),System.DateTime.Now.Ticks.ToString(),
"",false);
四。
点击收藏按钮时候弹出一个文本框;并且带有输入文本框;
所使用ajax控件;!
前台代码:
#region
PanelID="CollectionPnl"runat="server"Visible="false"> LinkButtonID="collectionLHyBtn"runat="server"Text="加入收藏" onclick="collectionLBtn_Click"> LinkButton> ButtonID="CollectionBtn"runat="server"CausesValidation="false"Style="display: none"/> ModalPopupExtenderID="ModalPopupExtender"runat="server"BackgroundCssClass="modalBackground" Drag="true"PopupDragHandleContro
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 收藏 遇到 问题
![提示](https://static.bdocx.com/images/bang_tan.gif)