实现web版用户注册(可以上传头像)
界面:
点击注册按钮后:
代码:
aspx中
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhuce.aspx.cs" Inherits="zhuce" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.tb
{
border:1px double green;
margin:0 auto;
}
#touxiang
{
width: 269px;
}
#btnzc
{
width: 71px;
}
.style1
{
width: 259px;
}
</style>
<script language="javascript" type="text/javascript">
// <![CDATA[
function btnzc_onclick() {
}
function btnzc_onclick() {
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="tb">
<tr><td>用户名:</td><td class="style1">
<asp:TextBox ID="TextBox1" runat="server" Width="272px"></asp:TextBox>
</td></tr>
<tr><td>密码:</td><td class="style1">
<asp:TextBox ID="TextBox2" runat="server" Width="271px" TextMode="Password"></asp:TextBox>
</td></tr>
<tr><td>确认密码:</td><td class="style1">
<asp:TextBox ID="TextBox3" runat="server" Width="270px" TextMode="Password"></asp:TextBox>
</td></tr>
<tr><td>性别:</td><td class="style1"> <input type="radio" runat="server" id="rdb" name="sex" />男 <input type="radio" runat="server" id="rdg" name="sex" />女</td></tr>
<tr><td>头像:</td><td class="style1"><input type="file" runat="server" id="touxiang" /></td></tr>
<tr><td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="注册"
Width="70px" />
</td></tr>
</table>
<br /><br /><hr />
<div id="result" runat="server">
</div>
</div>
</form>
</body>
</html>
cs中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
public partial class zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("用户名是:");
string YH = TextBox1.Text;
sb.Append(YH);
sb.Append("<br/>");
sb.Append("密码是:");
string MM1 = TextBox2.Text;
string MM2 = TextBox3.Text;
if (MM1 == MM2)
{
sb.Append(MM1);
}
else
{
TextBox3.Text = "两次输入密码不同请重新输入。";
}
sb.Append("<br/>");
sb.Append("性别为:");
if (rdb.Checked == true)
{
sb.Append("男");
}
else
{
sb.Append("女");
}
sb.Append("<br/>");
sb.Append("头像为:");
string Filename = this.touxiang/*这是input中file控件的id*/.PostedFile/*找到你选择上传的文件*/.FileName/*获取文件名*/;//此处是为了获取你选择的图片的文件名。
Filename = DateTime.Now.Ticks.ToString()/*获取当前时间*/ + Filename.Substring(Filename.LastIndexOf("."))/*获取你所选择的图片文件的扩展名*/;//从新给你选择的图片文件定义一个文件名。
string path = Server.MapPath("touxiang");
touxiang.PostedFile.SaveAs(path + @"\" + Filename);//将重新命名过的图片文件存入文件夹中
sb.Append("<img src=\"touxiang/" + Filename + "\"/>");
//result.InnerText= sb.ToString(); //使用InnerText的话<br/>等。。就使用不了了
result.InnerHtml = sb.ToString();
//File.Create("D:\\1111.txt");
//string[] aa={};
//for (int i = 0; i < Filename.Length; i++) {
// aa[i] = Filename[i].ToString();
//}
//File.WriteAllLines("D:\\1111.txt", aa);
}
}