.NET程序设计实验报告
20##年 10月27日
《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 />";
}
}
}
[1] 李锡辉,王樱等. ASP.NET网站开发实例教程[M]. 北京:清华大学出版社. 2011.3
学网页设计之心得体会
实验报告学院系名称计算机与通信工程学院第1页共9页第2页共9页第3页共9页第4页共9页第5页共9页第6页共9页第7页共9页第8页共…
实验一环境配置与sql操作学号姓名日期20xx年11月251实验目的熟悉VS20xx开发环境复习C和SQL20xx相关操作2实验环…
NET上机实验报告WEB数据库应用程序设计姓名杨森班级计103学号10101020xx2实验成绩一实验目的通过对aspnet学习完…
燕山大学Net程序设计实验报告学院信息科学与工程学院年级专业08级计算机科学2班指导教师崔永强学生学号080104010108学生…
河南城建学院网站建设技术实验报告实验名称用户名密码登录切图制作网站专业班级软件工程姓名吴春龙学号084413147实验日期20xx…
20xx13137028陈旻实计科1301ASPNET网站开发实验指导老师柯鹏学院计算机学院学生姓名陈旻实学生学号20xx1313…
实验一1实验要求基本的网页制作2目的掌握HTML与ASPX基础联系制作基本的ASPNET网页使用VS20xx提供的导航控件并结合站…
实验一环境配置与sql操作学号姓名日期1实验目的熟悉VS20xx开发环境复习C和SQL20xx相关操作2实验环境WindowsXP…
燕山大学Net程序设计实验报告学院信息科学与工程学院年级专业09级信息安全1班指导教师崔永强学生学号090104070017学生姓…
丽水职业技术学院机电信息分院课程实训基于ASP.NET旅游导购网站的设计与开发学生学号:学生姓名:实训老师:班级信管1222专业名…