📄 addtofavorite.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace BookStore
{
/// <summary>
/// AddToFavorite 的摘要说明。
/// </summary>
public class AddToFavorite : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.HyperLink HyperLink3;
protected System.Web.UI.WebControls.HyperLink HyperLink4;
protected System.Web.UI.WebControls.HyperLink HyperLink5;
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.DataList dlMyFavorite;
protected System.Web.UI.WebControls.HyperLink HyperLink6;
string strSql;
static int nUserID;
protected System.Web.UI.WebControls.Label lbInfo;
int nBookID;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
if(Object.Equals(Session["UserName"],null))
{
Session["ReqestedURL"] = Request.RawUrl;
Response.Redirect("../Error.aspx");
}
else
{
nUserID = int.Parse(Session["UserID"].ToString());
// If request from a bookdetail page, then first query and add it to favorite database
if(!Object.Equals(Request["BookID"],null))
{
nBookID = int.Parse(Request["BookID"].ToString());
strSql = "select UserID from [FavoriteBook] where UserID=" + nUserID + "And BookID=" + nBookID;
try
{
// If has the book already, then skip add it
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForValue(strSql);
}
catch
{
// otherwise, add to favorite
strSql = "INSERT INTO [FavoriteBook]([UserID], [BookID]) VALUES(" + nUserID + "," + nBookID + ")";
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
}
catch
{
this.lbInfo.Text = "插入数据失败!";
}
}
}
BindData();
}
}
}
private void BindData()
{
strSql = "select * from FavoriteBookDetail where UserID=" + nUserID;
DataSet currentDS;
currentDS = RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForDS(strSql);
this.dlMyFavorite.DataSource = currentDS.Tables[0].DefaultView;
this.dlMyFavorite.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dlMyFavorite.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMyFavorite_ItemCommand);
this.dlMyFavorite.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlMyFavorite_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dlMyFavorite_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int intBookID = int.Parse(e.CommandArgument.ToString());
DeleteData(intBookID);
BindData();
}
private void dlMyFavorite_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if(e.CommandName == "OpenBook")
{
string url;
url = "../ShowBookDetail.aspx?ID=" + e.CommandArgument.ToString();
Response.Redirect(url);
}
if(e.CommandName == "Buy")
{
string url;
url = "../Payment/AddToCart.aspx?BookID=" + e.CommandArgument.ToString();
Response.Redirect(url);
}
}
private void DeleteData(int intBookID)
{
string strSql = "DELETE FROM [FavoriteBook] where BookID=" + intBookID + " and UserID=" + nUserID;
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
this.lbInfo.Text = "删除成功";
}
catch
{
this.lbInfo.Text = "删除失败!";
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -