cookie的读和写
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
{
string username = Request.Cookies["username"].Value;
string password = Request.Cookies["password"].Value;
if (username == "zpp" && password == "111")
{
Response.Redirect("WebIndex.aspx");
}
}
if (Request.Cookies["username"] != null)
{
string cusername = Request.Cookies["username"].Value; // 读cookie
txbusername.Text = cusername;
}
//aotuman();
}
}
//private void aotuman()
//{
// if (ckautoman.Checked == true)
// {
// HttpCookie cookie = new HttpCookie("username" );
// cookie.Value = txbusername.Text;
// cookie.Expires = DateTime.Now.AddMinutes(5);
// Response.Cookies.Add(cookie);
// //写密码
// HttpCookie password = new HttpCookie("password");
// cookie.Value = txbPassword.Text;
// cookie.Expires = DateTime.Now.AddMinutes(5);
// Response.Cookies.Add(cookie);
// Response.Redirect("WebIndex.aspx");
// }
//}
protected void btnLogon_Click(object sender, EventArgs e)
{
if (txbusername.Text == "zpp" && txbPassword.Text == "111")
{
//写cookie
if (ckremember.Checked == true)
{
HttpCookie cookie = new HttpCookie("username");
cookie.Value = txbusername.Text;
//cookie.Value = txbPassword.Text;
cookie.Expires = DateTime.Now.AddMinutes(5);
Response.Cookies.Add(cookie);
}
if (ckautoman.Checked == true)
{
HttpCookie username = new HttpCookie("username");
username.Value = txbusername.Text;
username.Expires = DateTime.Now.AddMinutes(5);
Response.Cookies.Add(username);
HttpCookie password = new HttpCookie("password");
password.Value = txbPassword.Text;
password.Expires = DateTime.Now.AddMinutes(2);
Response.Cookies.Add(password);
//Response.Redirect("WebIndex.aspx");
}
}
Response.Redirect("WebIndex.aspx");
}
点击按钮,用cookies保存
protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
if (Request.Cookies["productname"] != null)
{
HttpCookie cookie = new HttpCookie("productname");
cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
else
{
HttpCookie cookie = new HttpCookie("productname");
cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
Response.Redirect("WebProduct3.aspx");
}
运用数据库从数据中调出数据
public partial class WebProduct2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (String.IsNullOrEmpty(Request.QueryString["productname"]) == false)
{
string productname = Request.QueryString["productname"]; //从另一个界面传值
getProductname(productname);
}
}
}
private void getProductname(string productname)
{
string sqlstr = "Data Source=SXVPTNJT9Q07YYP;Initial Catalog=Student2;User ID=sa;Password=abcdef";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(sqlstr))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_SaleProduct where ProductName=@productname";
cmd.Parameters.AddWithValue("@productname",productname);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
}
}
this.imgproduct.Src = dt.Rows[0]["ProductImage"].ToString();
this.spproductname.InnerText = dt.Rows[0]["ProductName"].ToString();
this.Sppruductprice.InnerText = dt.Rows[0]["Price"].ToString();
}
protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
if (Request.Cookies["productname"] != null)
{
HttpCookie cookie = new HttpCookie("productname");
cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
else
{
HttpCookie cookie = new HttpCookie("productname");
cookie.Value += this.imgproduct.Src + "," + this.spproductname.InnerText + "," + this.Sppruductprice.InnerText + "|";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
Response.Redirect("WebProduct3.aspx");
}
protected void imgBtnBuy_Click(object sender, ImageClickEventArgs e)
{
if (txbNumber.Text != null && txbNumber.Text != "0")
{
double money = Convert.ToDouble(Sppruductprice.InnerText) * Convert.ToDouble(txbNumber.Text);
string sqlstr = "Data Source=SXVPTNJT9Q07YYP;Initial Catalog=Student2;User ID=sa;Password=abcdef";
int result = 0;
using (SqlConnection conn = new SqlConnection(sqlstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_BuyProduct values(@username,@totalmoney)";
cmd.Parameters.AddWithValue("@username",Session["Username"].ToString());
// cmd.Parameters.AddWithValue("@address",Session["Address"].ToString());
cmd.Parameters.AddWithValue("@totalmoney", money);
result= cmd.ExecuteNonQuery();
}
}
if (result > 0)
{
Response.Write("交易成功!");
}
else { Response.Write("交易不成功,请重新购买!"); }
}
}
}