📄 search.aspx.cs
字号:
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;
public partial class MasterPage_search : System.Web.UI.Page
{
UserInfoClass ucObj = new UserInfoClass();
DBClass db=new DBClass();
SqlConnection con = new SqlConnection();
private const String SELECT_SQL = "select top {0} * from tb_GoodsInfo where GoodsID not in (select top {1} GoodsID from tb_GoodsInfo order by GoodsID) order by GoodsID";
private const String GET_ROWCOUNT = "select count(GoodsID) from tb_GoodsInfo";
private const Int32 pageSize = 10;
private const Int32 showPageNumberCount = 7;
private Int32 totalRecordCount = 0;
// protected System.Web.UI.HtmlControls.HtmlGenericControl lblTime;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//调用
selcet();
page();
}
}
//根据商品ID、名称、类别、会员价格日期按降序进行查询
public void selcet()
{
string search2 = Request.Form["search"];//参数传递非常重要
SqlConnection con = new SqlConnection();
con = db.GetConnection();
//查询语句(多表查询)
string mySelectQuery =
string.Format("select * from tb_GoodsInfo g, tb_Class c where g.ClassID=c.ClassID and (GoodsID like '%" + search2 + "%' or GoodsName like '%" + search2 + "%' or ClassName like '%" + search2 + "%'or MemberPrice like '%" + search2 + "%') order by AddDate desc");
SqlDataAdapter da = new SqlDataAdapter(mySelectQuery, con);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds);
search1.DataSource = ds.Tables[0].DefaultView;
search1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
//绑定市场价格
public string GetMKPStr(string P_Str_MarketPrice)
{
return ucObj.VarStr(P_Str_MarketPrice, 2);
}
//绑定会员价格
public string GetMBPStr(string P_Str_MemberPrice)
{
return ucObj.VarStr(P_Str_MemberPrice, 2);
}
protected void search1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "detailSee")
{
Session["address"] = "";
Session["address"] = "index.aspx";
Response.Redirect("~/User/GoodsDetail.aspx?GoodsID=" + Convert.ToInt32(search1.DataKeys[e.Item.ItemIndex].ToString()));
}
else if (e.CommandName == "buyGoods")
{
AddShopCart(e, search1);
}
}
//当购买商品时,获取商品信息
public SaveSubGoodsClass GetSubGoodsInformation(DataListCommandEventArgs e, DataList DLName)
{
//获取购物车中的信息
SaveSubGoodsClass Goods = new SaveSubGoodsClass();
Goods.GoodsID = int.Parse(DLName.DataKeys[e.Item.ItemIndex].ToString());
string GoodsStyle = e.CommandArgument.ToString();
int index = GoodsStyle.IndexOf("|");
if (index < -1 || index + 1 >= GoodsStyle.Length)
return Goods;
Goods.GoodsWeight = float.Parse(GoodsStyle.Substring(0, index));
Goods.MemberPrice = float.Parse(GoodsStyle.Substring(index + 1));
return (Goods);
}
public void AddShopCart(DataListCommandEventArgs e, DataList DLName)
{
if (Session["UID"] != null)
{
SaveSubGoodsClass Goods = null;
Goods = GetSubGoodsInformation(e, DLName);
if (Goods == null)
{
//显示错误信息
Response.Write("<script>alert('没有可用的数据');</script>");
return;
}
else
{
ucObj.AddShopCart(Goods.GoodsID, Goods.MemberPrice, Convert.ToInt32(Session["UID"].ToString()), Goods.GoodsWeight);
Response.Write("<script>alert('恭喜您,添加成功!')</script>");
}
}
else
{
Response.Write("<script>alert('请先登录,谢谢合作!');</script>");
}
}
//分页
public void page()
{
String currentIndex = Request.QueryString["id"] ?? "1";
DataTable dt = new DataTable();
con = db.GetConnection();
SqlDataAdapter sda = new SqlDataAdapter(
String.Format(SELECT_SQL, pageSize, pageSize * (int.Parse(currentIndex) - 1)),
con
);
sda.Fill(dt);
SqlCommand cmd = new SqlCommand(GET_ROWCOUNT, con);
con.Open();
totalRecordCount = (int)cmd.ExecuteScalar();
AfengPager2.ShowRecordCount = pageSize; // 设定每页显示的记录数
AfengPager2.ShowPageNumberCount = showPageNumberCount; // 设定显示的页码数
AfengPager2.TotalRecordCount = totalRecordCount; // 设定数据库记录总行数
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -