您的位置:首页 > 移动开发

.NET高级应用(第1弹)

2014-03-25 21:25 239 查看
虽然小学期学过了C#和ASP.NET,然后就跟着一些学长开始做一些项目,但是总感觉不是很扎实。接下来这四周我们将学习.NET高级应用,我想把课上老师讲的一些内容总结一下分享给我带的一些小学弟(本人也很菜,所以希望能对他们有帮助)。

前方很水,大神绕行!欢迎各路神仙批评指正嘿嘿!

1.简单的用户信息录入显示(熟悉工具栏中控件的使用)。

                 举例如图所示:



            用到的控件有如下几个:

TextBox ——输入文本信息如图“用户名”“简介”,命名格式分别为“txtName”"txtIntroduce"其中简介是修改属性“TextMode”为MultiLine

RadioButton——定义单选按钮组,命名格式为“rdoGender”,其中“RepeatDirections”调整排列方式。

CheckBoxList——定义复选按钮,命名格式为“chkLove”,其中“RepeatDirections”调整排列方式。

DropDownList——下拉菜单选项,命名格式为“drpDegree”

Label——"lblMsg"命名格式

Button——“btnSubmit”命名格式。

CheckBox——“记住密码的使用”

双击"Button"进入后台代码编辑(蓝色是需要注意的地方):

string message = "";//定义空的字符串
message += "用户名是:" + txtName.Text;
message += "<br>用户性别:" +rdoGender.SelectedValue;//<br>换行号
message += "<br>用户的爱好是:";
string strTemp = "";
for (int i = 0; i < chkLove.Items.Count; i++)
{
if (chkLove.Items[i].Selected)
{
strTemp += chkLove.Items[i].Value + "";
}
}
message += strTemp;//CheckBox的使用
message += "<br>用户的学位是:"+drpDegree.SelectedItem.ToString();//GropDownList获取参数值
message += "<br>用户的个人简介是:" + txtIntroduce.Text;
this.lblMsg.Text = message;//this.lblMsg.Text=message;的使用

2.登录界面的实现

         需要注意的地方(记住密码):利用cookie实现密码记忆功能,将用户名和密码记录到Cookie中

关键是要设置Cookie对象的Expires属性和Value属性

protected void btnLogin_Click(object sender, EventArgs e)
{
string strName = txtName.Text.Trim();
string strPwd = txtPwd.Text.Trim();
if (strName == "Bush" && strPwd == "123")
{
if (chkRember.Checked)//记住密码
{
if (Request.Cookies["username"] == null)
{
Response.Cookies["username"].Value = strName;//session 内存 cookie不安全
Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);//设置cookie对象的有效时间
}
}
Session["name"] = strName;//用户名存储到Session对象中
Response.Redirect("Success.aspx");//url攻击,加密,解密;

}
else
{
Response.Write("<script>alert('用户名密码错误')</script>");
}
}

再次登陆的时候有:

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["username"] != null)//如果存在指定的Cookie的对象
{
Session["name"]=Request.Cookies["username"].Value;//获取Cookie对象中存储的密码
Response.Redirect("Success.aspx");
}

}

相应的Success.aspx界面后台代码:
if (Session["name"] != null)
{
string strName = Session["name"].ToString();
Label1.Text = "欢迎" + strName + "大驾光临";
}
else
{
Response.Redirect("login.aspx");
}


3.简单聊天室的实现。

新建三个web窗体,分别命名为Say.aspx,Show.aspx,Index.aspx

Say.aspx前台简单框体



双击Button进入后台实现。

Application.Lock();
if (Application["content"] == null)
{
Application["content"] = "";
}
Application["content"] = txtSay.Text+"<br>"+Application["content"].ToString();
Application.UnLock();
txtSay.Text = "";


然后再show.aspx界面前台拖拽一个lable控件,命名为lblMsg,需要注意的是

<title></title>

   <meta http-equiv="refresh" content="5" />

</head>部分完成刷新机制

后台实现

if (Application["content"] != null)
lblMsg.Text = Application["content"].ToString();
Index.aspx前台添加

</head>

<frameset rows="*,80">

    <frame src="show.aspx"></frame>

    <frame src="say.aspx"></frame>

 </frameset>  

</html>

框架机制

结束!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息