web对象的使用

来源:互联网 发布:淘宝怎么做自己的app 编辑:程序博客网 时间:2024/04/29 05:39
 

简易聊天室:

protected void Page_Load(object sender, EventArgs e)

    {

        Label say = new Label();

        say.Text = Application["content"].ToString();

        this.Panel1.Controls.Add(say);

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        string a = null;

        a = Request["names"];

        Application["content"] =Application["content"]+ a + "\n" + ":" + "<img src='image/" + DropDownList1.SelectedValue + ".gif'>" + this.TextBox1.Text + "<br/>" ;

}

站点计数器:

protected void Page_Load(object sender, EventArgs e)

    {

        int nums = (int)Application["nums"];

        nums++;

        Application.Lock();

        Application["nums"] = nums;

        int sum = 0;

        Label la = new Label();

        do

        {

            sum = nums % 10;

            Application["content"] = "<img src='image/" + sum.ToString() + ".png'/>";

            nums=nums/10;       

            la.Text=Application["content"].ToStrin

g()+la.Text;

        } while (nums!=0);//显示数字图片

        this.Panel1.Controls.Add(la);

        Application.UnLock();

    }

Session对象和Cookies对象

if (this.TextBox1.Text.Trim()!="张三"|this.TextBox2.Text!="asdf")

        {

            Response.Write("输入有误!");

        }

        else

        {

            HttpCookie cookie = new HttpCookie("username",this.TextBox1.Text);

//利用Cookie实现自动欢迎信息(一次登陆之后,以后每次登陆自动获取原来的登陆信//息)

            cookie.Expires = DateTime.Now.AddHours(1);

            this.Response.Cookies.Add(cookie);

            Session["username"] = this.TextBox1.Text;

            Response.Redirect("Default2.aspx");

        }

//实现网络安全性

 if (Session["username"] == null)

            this.Response.Redirect("default.aspx", true);

        this.Literal1.Text = this.Session["username"].ToString();

        this.Literal1.Text = "欢迎你" + Session["username"];//通过Session对象传递信息