.net实验报告

.NET程序设计实验报告

                                                     20##年 10月27日


 

第二篇:ASPNET课程实验报告

ASP.NET程序设计》课程实验报告

实验课题:                            

             计算机应用        

    级:                      

指导教师                       

完成人:

2011   5 29


5.3 留言板

public partial class liuyan : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (Session["userID"] != null)

        {

            if (TextBox1.Text == "" || TextBox2.Text == "")

            {

                Page.RegisterStartupScript("", "<script>alert('请输入留言主题和内容!');</script>");

            }

            else

            {

                try

                {

                    string connstr = ConfigurationManager.ConnectionStrings["WShopConnectionString"].ConnectionString;

                    SqlConnection conn = new SqlConnection(connstr);

                    conn.Open();

                    SqlCommand cmd = new SqlCommand("Proc_InsertMessageInfo", conn);

                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter para1 = new SqlParameter("@userID", SqlDbType.Int);

                    para1.Value = Session["userID"].ToString();

                    cmd.Parameters.Add(para1);

                    SqlParameter para2 = new SqlParameter("@Heading", TextBox1.Text);

                    cmd.Parameters.Add(para2);

                    SqlParameter para3 = new SqlParameter("@Content", SqlDbType.NVarChar);

                    para3.Value = TextBox2.Text;

                    cmd.Parameters.Add(para3);

                    SqlParameter para4 = new SqlParameter("@Time", SqlDbType.DateTime);

                    para4.Value = DateTime.Now.ToShortDateString();

                    cmd.Parameters.Add(para4);

                    SqlParameter para5 = new SqlParameter("@flag", DbType.Int32);

                    para5.Direction = ParameterDirection.Output;

                    cmd.Parameters.Add(para5);

                    cmd.ExecuteNonQuery();

                    Page.RegisterStartupScript("", "<script>alert('发表成功!');</script>");

                    conn.Close();

                }

                catch

                { }

            }

        }

        else

        {

            Response.Redirect("denglu.aspx");

        }

        Response.Redirect("liuyan.aspx");

    }

}

5.4 商品查询

protected void Button1_Click(object sender, EventArgs e)

    {

        SqlDataSource2.SelectParameters.Clear();

        string str = SqlDataSource2.SelectCommand + "where Waretype.typeID=@typeID";

        Parameter para1 = new Parameter("typeID", DbType.Int16);

        para1.DefaultValue = DropDownList1.SelectedItem.Value;

        SqlDataSource2.SelectParameters.Add(para1);

        if (TextBox1.Text != "")

        {

         str+="and Ware.Warename like '%'+@Warename+'%'";

         SqlDataSource2.SelectParameters.Add("Warename", TextBox1.Text);

        }

        GridView1.DataSourceID = null;

        SqlDataSource2.SelectCommand = str;

        GridView1.DataSourceID = "SqlDataSource2";

}

5.5 在线聊天

protected void Page_Load(object sender, EventArgs e)

    {

        if (Session["uName"] != null)

        {

            lblOnlineNum.Text = "当前在线人数为" + Application["count"].ToString() + "人";

            txtChatRoom.Text = Application["chat"].ToString();

            lblName.Text = Session["uName"].ToString();

        }

        else

        {

            Response.Redirect("denglu.aspx");

        }

    }

    protected void btnSend_Click(object sender, EventArgs e)

    {

        string tab = "";

        string newline = "\r";

        string newMessage = lblName.Text + ";" + tab + txtChat.Text + newline + Application["chat"];

        if (newMessage.Length > 500)

            newMessage = newMessage.Substring(0, 499);

        Application.Lock();

        Application["chat"] = newMessage;

        Application.UnLock();

        txtChat.Text = "";

        txtChatRoom.Text = Application["chat"].ToString();

    }

5.6 在线投票

public partial class toupiao : System.Web.UI.Page

{

    ArrayList cn = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)

    {

        //validatorlogin.Validator();

        if (Request.Cookies["vote"] != null)

        {

            lblState.Text = "<b>您已经投过票了!</b>";

        }

        else

        {

            lblState.Text = "<b>您可以投票!</b>";

        }

        getVote();

    }

    protected void getVote()

    {

        string filePath = Server.MapPath("vote.txt");

        try

        {

            StreamReader sr = File.OpenText(filePath);

            string str = sr.ReadLine();

            string[] strvote = str.Split(',');

            foreach (string ss in strvote)

                cn.Add(int.Parse(ss));

            sr.Close();

        }

        catch (Exception ee)

        {

            Response.Write("<script>alert('" + ee.Message + "')</script>");

        }

    }

    protected void putVote()

    {

        string filePath = Server.MapPath("vote.txt");

        try

        {

            StreamWriter sw = new StreamWriter(filePath, false);//false表示不是追加的

            string str = cn[0].ToString();

            for (int i = 1; i < cn.Count; i++)

            {

                str += "," + cn[i].ToString();

            }

            sw.WriteLine(str);

            sw.Close();

        }

        catch (Exception ee)

        {

            Response.Write("<script>alert('" + ee.Message + "')</script>");

        }

    }

    protected void btnVote_Click(object sender, EventArgs e)

    {

        if (rbtlVote.SelectedIndex != -1)

        {

            if (Request.Cookies["vote"] == null)

            {

                int i = rbtlVote.SelectedIndex;

                cn[i] = int.Parse(cn[i].ToString()) + 1;

                putVote();

                Response.Cookies["vote"].Value = "vt";

                Response.Cookies["vote"].Expires = DateTime.Now.AddDays(14);

                Response.Write("<script>alert('投票成功!')</script>");

            }

            else

            {

                Response.Write("<script>alert('您已经投过票了!')</script>");

            }

        }

        else

        {

            if (lblState.Text == "您已经投过票了!")

            {

                Response.Write("<script>alert('您已经投过票了!')</script>");

            }

            else

            {

                Response.Write("<script>alert('请选择投票项!')</script>");

            }

        }

    }

    protected void btnView_Click(object sender, EventArgs e)

    {

        lblView.Text = "各品牌的票数为:<br />";

        for (int i = 0; i < cn.Count; i++)

        {

            lblView.Text += rbtlVote.Items[i].Text + ":" + cn[i].ToString() + "票<br />";

        }

    }

}

7. 参考文献

[1] 李锡辉,王樱等. ASP.NET网站开发实例教程[M]. 北京:清华大学出版社. 2011.3

学网页设计之心得体会

相关推荐