关于cookie读值问题,为什么老是读不到某一个值!!
private void Button1_Click(object sender, System.EventArgs e)
{
//格式:<ID数><ID数2>,使用"<>"符可使ID数值对比时定位准确
if(RB.SelectedIndex!=-1)//如果有选定项目
{
//----------------------- 投票防作弊 -----------------------
HttpCookie makecookie=new HttpCookie ("SocutVote");//制造cookie
HttpCookie readcookie=Request.Cookies["SocutVote"];//读出cookie
if(readcookie==null)//从未投过票
{
makecookie.Values.Add ("VoteItem","<"+strID+">");
makecookie.Expires=DateTime.Now.AddDays(1);
//makecookie.Path="/";
//Label1.Text ="成功!";
// IPlist.Add(lbShowIP.Text);
// bindIPtoListBox();
//Response.Redirect("result.aspx?ID="+strID);//查看结果
//Response.Redirect("result.aspx?ID="+strID);//查看结果
//Response.Write("<script>window.open('result.aspx?ID="+strID+"',target='_blank');</script>");
}
else
{
strAllItem=readcookie.Values["VoteItem"].ToString ();//读取已投票的项
if(strAllItem.IndexOf("<"+strID+">")==-1)//未投过票
{
makecookie.Values.Add("VoteItem",readcookie.Values["VoteItem"]+"<"+strID+">");
}
else//如果已投过票
{
Response.Redirect("error.aspx");
//Label1.Text="<h5 style='color:red'>出错:您已投过票!</h5>";
//Page.RegisterStartupScript("","<script>alert('你已经投过票了!')</script>");
}
}
Response.Cookies.Add(makecookie);//写入Cookie
//Label1.Visible=false;
//==================================================================================
string strSelect=strContent.Split('|')[Convert.ToInt32 (RB.SelectedValue.ToString())];//当前选中的项目,如“ 2,不错”
int intVote=Convert.ToInt32 (strSelect.Split(',')[0]);//当前投票数+1
int intVoteOK=intVote+1;
string strSelectOK=strSelect.Replace (intVote.ToString (),intVoteOK.ToString ());//更新原投票数,所以期望更新的定位精确,可附带+","
string strUPDATE=strContent.Replace(strSelect,strSelectOK);//得到最终
myData.GetExecuteNonQuery("UPDATE socut_vote SET v_content='"+strUPDATE+"' WHERE v_id="+strID);
}
else//如果没有选中项目
{
Label1.Text ="<h5 style='color:red'>出错:请选择投票项!</h5>";
}
}
如果用//Label1.Text="<h5 style='color:red'>出错:您已投过票!</h5>";这个就会报strAllItem=readcookie.Values["VoteItem"].ToString ();//读取已投票的项这个cookie为空。如果用Response.Redirect("error.aspx");这句就不会有错。而且还能写入cookie用label提示错误就连COOKIE也写不入了。还会删除掉cookie,望大侠们指教!!