Cpp Lover

整理知识,记录成长轨迹

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

因为在做注册页面,点击注册按钮完成以下任务:

①把得到的注册信息,通过后台添加到数据库中,

②在前台显示“注册成功”,

③在注册的名字,性别写进cookie中

刚开始想着所有的东西都在后台完成,但是发现一个问题,在后台产生的cookie是在一级域名中的,我做的在二级域名中,需要后台修改,查找了不少资料但是无法成功。

 

后台代码如下:

 

 //设置条件Cookie  
    private void SetWhereCookie(string where)
    {
        HttpCookie cookie 
= new HttpCookie("username");
        cookie.Value 
= where;
        cookie.Expires 
= DateTime.Now.AddDays(1d);        

        Response.Cookies.Add(cookie);
    }


protected void Button1_Click(object sender, EventArgs e)
    {
 username 
= TextBox1.text;
SetWhereCookie(username );
Response.Write(
"<script> if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");
}

 

 

 但是遇到前端写的cookie就可以在domain中直接带着二级域名,而后台写的却不可以。

前台生成的cookie文件
sex
2
localhost
/jingsystem/
1600
1488151808
30066602
237686608
30066552
*
username
sssssaaaqw
localhost
/jingsystem/
1600
88282624
30066606
3136544720
30066555
*
后台生成的cookie文件
sex
2
localhost
/
1600
1488151808
30066602
237686608
30066552
*
username
sssssaaaqw
localhost
/
1600
88282624
30066606
3136544720
30066555
*

找到了一种解决办法,在后台的cookie代码中添加path来控制cookie.Path="/jingsystem/",总算把这个问题解决。

 

还有一套解决方案是,前台写好方法,后台来调用。

前端代码如下:

 

function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{
var Days 
= 0.25//此 cookie 将被保存 30 天
var exp   = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie 
= name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function writeCookie()
  {
      alert(
"hehe");
       SetCookie (
"username", document.getElementById ("textusername").value); 
       var obj 
= document.getElementsByName(RadioButtonList1); 
        
for(i = 0;  i <obj.length;i++)    
        {         
if(obj[i].checked)   SetCookie ("sex",i);       
                      
        }          
  }

 

 

 

后台代码:

 

protected void Button1_Click(object sender, EventArgs e)
    {
 username 
= TextBox1.text;
ClientScript.RegisterStartupScript(ClientScript.GetType(), 
"myscript""<script>writeCookie();if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");                
Response.Write(
"<script> if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");
}

 

 

又遇见前一句不执行问题,所以只好修改后台代码如下:

 ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>writeCookie();if (confirm('注册成功,您将前往首页进行登录!'))  window.location.href='./Homepage.aspx';</script>");

 

终于搞定!

还有几个问题存在着疑惑:

①为什么前端写的cookie就可以在domain中直接带着二级域名,后台写的必需得加上path。

②ClientScript.RegisterStartupScript与response.write前后两句写,前面不执行。

 

 

 

 

此外推荐三篇在解决问题中找到的好文章:

某位高手写的后台一个很实用的类:http://blog.csdn.net/zhoufoxcn/archive/2008/04/21/2312440.aspx

服务器调用前台代码的总结文章:http://www.cnblogs.com/lhuser/articles/1458825.html

asp.net中js与c#为互相访问:http://www.cnblogs.com/ywqu/archive/2009/01/08/1371483.html

有关cookie的属性讲解   http://www.webdn.com/web_file/program/asp/N0610925/

 

posted on 2010-03-19 21:54  quanhailee  阅读(2402)  评论(4编辑  收藏  举报