asp.net 作业合集一

asp.net 作业合集一
实验4 ASP.NET内置对象的使用
实验类型:设计性实验 要求:必做 学时:4
实验时间及地点:

实验目的
1、掌握Response、Request、Server对象的常用方法;
2、掌握Cookie信息、Session信息、Application信息的建立与使用方法;
实验内容
1、使用Redirect方法实现页面跳转。
2、使用Request对象获取页面间传送的值。
3、Server对象的MapPath方法使用。
4、利用Cookie实现密码记忆功能。
5、使用Session对象进行页面间传值。
6、统计网站总访问量。
7、制作一个简单的在线聊天室。
实验步骤
1、使用Redirect方法实现页面跳转。
设计一个页面,在后台先判断当前日期是偶数还是奇数,如果是偶数,则跳转至新浪网站,否则跳转至搜狐网站。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "";
        int day = DateTime.Now.Day;
        if(day % 2 == 0 )
        {
            url = "http://www.sina.com.cn";
        }
        else
        {
            url = "http://www.sohu.com";
        }
        Response.Redirect(url);
    }
}

2、使用Request对象获取页面间传送的值。
设计2个页面,在第一个页面里输入用户名,在第二个页面里利用Request[“元素名”]来获取第一个页面里输入的用户名。

 string uname = Request.QueryString["Textbox1"];
        Response.Write(uname);

3、使用Server对象的MapPath方法获得物理地址。
1)新建窗体页面,参考下图进行前台界面设计:

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Server.MapPath("~");
        Label2.Text = Server.MapPath("~\\Default2/aspx");
    }
}

2)后台代码参考:

4、利用Cookie实现密码记忆功能。
1)新建页面,设计一个用户登录的界面,用Cookies保存登录信息。

2)说明:选择记住密码的checkbox,就创建一个cookie用于记录密码的内容,同时设置有效期。当下次加载的时候,判断有没有这个密码cookie,如果有再判断这个cookie是否过期,若未过期,就将这个cookie里存的值取出来,放到对应的文本框中。建议把有效期设置为10s,原因是为了看到的效果明显些。在20s之前,密码部分还是一直有值,过了20s,就自动清空了,这是因为cookie到期了。

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(null!=Request.Cookies["uname"]&&null!=Request.Cookies["pwd"])
        {
            TextBox1.Text = Request.Cookies["uname"].Value;
            TextBox2.Attributes["Value"] = Request.Cookies["pwd"].Value;
            CheckBox1.Checked = true;
        }
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if(CheckBox1.Checked)
        {
            Response.Cookies["uname"].Value = TextBox1.Text;
            Response.Cookies["pwd"].Value = TextBox2.Text;
            Response.Cookies["uname"].Expires = DateTime.Now.AddSeconds(20);
            Response.Cookies["pwd"].Expires = DateTime.Now.AddSeconds(20);
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        CheckBox1.Checked = false;
        
    }
}

5、使用Session对象进行页面间传值。
设计2个页面,在第一个页面里输入用户名和密码,存到Session变量中,在第二个页面中通过访问第一个页面建立的Session变量,获取第一个页面里输入的用户名和密码。

<body>
    <form id="form1" runat="server">
        <div>
        <p>
            用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            密码 : 
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登录" />
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="重置" />
        </p>
        </div>
    </form>
</body>



public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if ("" != TextBox1.Text && "" != TextBox2.Text) ;
        {
            Session["uname"] = TextBox1.Text;
            Session["pwd"] = TextBox2.Text;
            Response.Redirect("Default7.aspx");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
}

6、统计网站总访问量。
通过Application对象和对文件的读写操作来统计网站的总访问量。新建全局应用程序类文件Global.asax,当应用程序启动时读取文件中的数据,将其值赋给Application对象。当新会话启动时,需要获取Application对象中的数据信息并使总访问量加1。当应用程序结束时,将已更改的总访问量存放在文件中。

 void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        Application["counter"] = 0;

        int count = 0;
        string count_path = Server.MapPath("./counter.txt");
        StreamReader srd = File.OpenText(count_path);
        while(-1 != srd.Peek())
        {
            string str = srd.ReadLine();
            count = int.Parse(str);
        }
        srd.Close();
        Application["counter"] = count;

    }

    void Application_End(object sender, EventArgs e)
    {
        //  在应用程序关闭时运行的代码
        int count = (int)Application["counter"];
        string count_path = Server.MapPath("./counter.txt");
        StreamWriter srw = new StreamWriter(count_path, false);
        srw.Write(count);
        srw.Close();
    }

    void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        // 在新会话启动时运行的代码
        Application.Lock();
        int count = (int)Application["counter"];
        count += 1;
        Application["counter"] = count;
        string count_path = Server.MapPath("./counter.txt");
        StreamWriter srw = new StreamWriter(count_path, false);
        srw.Write(count);
        srw.Close();

        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // 或 SQLServer,则不引发该事件。
    }
</script>


 protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Response.Write("网站总访问人数:" + Application["counter"].ToString());
        }
    }

7、制作一个简单的在线聊天室。
要求使用Application对象。新建全局应用程序类文件Global.asax,当应用程序启动时初始化计数器。设计效果如下图所示。

<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        Application["online"] = 0;
        Application["chat"] = "";
    }

    void Application_End(object sender, EventArgs e)
    {
        //  在应用程序关闭时运行的代码
    }

    void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码
    }

    void Session_Start(object sender, EventArgs e)
    {
        // 在新会话启动时运行的代码
        Application.Lock();
        Application["online"] = 1 + (int)Application["online"];
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // 或 SQLServer,则不引发该事件。
        Application.Lock();
        Application["online"] = (int)Application["online"] - 1;
        Application.UnLock();
    }
</script>



protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "" && TextBox1.Text != "")
        {
            Session["name"] = TextBox1.Text;
            Response.Redirect("default2.aspx");

        }
        else
            Response.Write("<script language='javascript'>alert('用户名或密码不能为空!');</script>");
    }

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] != null)
        {
            Label1.Text = "当前在线人数:" + Application["online"].ToString();
            TextBox1.Text = Application["chat"].ToString();
            Label2.Text = Session["name"].ToString();
            Response.AddHeader("refresh", "30");
        }
        else
            Response.Redirect("Default.aspx");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string newmessage = Session["name"] + ":" + DateTime.Now.ToString() + "\r" + TextBox2.Text + "\r" + Application["chat"];
        if (newmessage.Length > 500)
            newmessage = newmessage.Substring(0, 499);
        Application.Lock();
        Application["chat"] = newmessage;
        Application.UnLock();
        //Label2.Text = "";
        TextBox1.Text = Application["chat"].ToString();
        
    }
}

实验小结
我掌握Response、Request、Server对象的常用方法;
掌握Cookie信息、Session信息、Application信息的建立与使用方法;

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值