我写了一段测试代码,主要目的有3个,
1、Cookie的应用
2、Session的应用
3、事件的应用就这么简单的问题,居然一个都不通。整了很长时间,都不行,不知什么原因。
 
第一个是请求页面Query.aspx<%@ Page Language="C#" AutoEventWireup="true" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Query</title>
    
    <script language="C#" runat="server">    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie myck = Request.Cookies["email"];
        if (myck != null)
        {
            email.Text = myck.Values["value"].ToString();
            chkbox.Checked = bool.Parse(myck.Values["chk"].ToString());
        }
    } protected void Button_Click(object sender, EventArgs e)
    {
        Session["email"] = email.Text;        HttpCookie myck = new HttpCookie("email");
        if (chkbox.Checked)
        {
            myck.Values["value"] = email.Text;
            myck.Values["chk"] = chkbox.Checked.ToString();
            Response.Cookies.Add(myck);
        }
        else
        {
            Response.Cookies.Remove("email");
        }
    }</script>
    
</head>
<body>    <form runat="server" action="Result.aspx" method="get">
    <div>    <asp:Label ID="Label1" runat="server">输入注册邮件:</asp:Label>
    <asp:TextBox ID="email" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button_Click" Text="Button" />
    <br />
    <asp:CheckBox ID="chkbox" Text="记住邮件" runat="server"/>
    </div>
    </form>
 
</body>
</html>第两个页面Result.aspx
<%@ Page Language="C#" AutoEventWireup="true" EnableViewStateMac="false" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">
    <link href="StyleSheet.css" type="text/css" rel="Stylesheet" />
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <hr />
    <h2>Email:<%=Session["email"] %></h2>
    <hr />
    </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    debug,确定执行进入过你的逻辑
    action="Result.aspx"
    怀疑没有执行你的Query.aspx代码,而是直接跳走一般aspx都是提交到自己的页面的,
    而且一般不用修改form的属性
    另外不建议,把代码放到aspx文件中
      

  2.   

    - -!先不说别的 你能不能把前后台分开来啊 asp.net的诞生就是为了codebihind.你这样还按照asp的来- -!
      

  3.   

    这开发习惯。。值得批评,分开写。说不定就好了,而且action="Result.aspx"
    ,你前后台都写在一起,action="Result.aspx"传到哪?难道自己传给自己,前台aspx,后台aspx.cs,分开来写吧