最近学习了些服务器控件,写了一个学生信息采集的程序熟悉下各控件的用法:
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="studentinfo._Default" %>
<!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">
.divstyle
{
border-bottom-color:Green; border-bottom-width :1px; border-bottom-style:solid;
border-left-color:Green; border-left-width :1px; border-left-style:solid;
border-right-color:Green; border-right-width :1px; border-right-style:solid;
border-top-color:Green; border-top-width :1px; border-top-style:solid;
height:500pt;
width:700pt;
text-align:center;
vertical-align:middle;
margin:0 auto;
}
.style1
{
width: 86px;
}
.style2
{
width: 276px;
}
</style>
<script language="javascript" type="text/javascript" >
function test()
{
document.all.i.src=document.all.FileUpload1.value;
}
function CleckInput()
{
if(document.getElementById("username")=="")
{
alert("请输入姓名!");
document.getElementById("username").focus();
return false;
}
if(document.getElementById("num")=="")
{
alert("请输入学号!");
document.getElementById("num").focus();
return false;
}
if(document.getElementById("password")=="")
{
alert("请输入密码或更改错误!");
document.getElementById("password").focus();
return false;
}
var xb=0;
if (document.getElementById("xbnan").checked)
{
xb=1;
}
if (document.getElementById("xbnv").checked)
{
xb=1;
}
if(xb==0)
{
alert("请选择性别");
document.getElementById("xbnan").focus();
return false;
}
if (document.getElementById("DropDownList1").value=="")
{
alert("请选择班级");
document.getElementById("DropDownList1").focus();
return false;
}
if(document.getElementById("CheckBoxList1").value=="")
{
alert("请输入爱好");
document.getElementById("CheckBoxList1").focus();
return false;
}
if(document.getElementById("info").value=="")
{
alert("请填写简介!");
document.all.info.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server" >
<div class="divstyle" >
<table >
<tr><td>
</td></tr>
<tr><td> </td></tr>
<tr><td align="center" style="font-size:20px">学生信息采集</td></tr>
<tr><td> </td><td> </td></tr>
<tr><td> </td><td> </td></tr>
</table>
<table >
<tr ><td class="style1">学号:</td><td class="style2">
<asp:TextBox ID="num" runat="server"></asp:TextBox></td>
<td rowspan="5"><img src="" width="100px" />
</td></tr>
<tr><td class="style1" > </td><td class="style2"> </td></tr>
<tr><td class="style1"> 姓名:</td><td class="style2">
<asp:TextBox ID="username" runat="server"></asp:TextBox></td></tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr><td class="style1">密码:</td><td class="style2">
<asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox></td></tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr><td class="style1">性别:</td><td class="style2">
<asp:RadioButton ID="xbnan" runat="server" GroupName="sex" Checked="true"/>男 <asp:RadioButton
ID="xbnv" runat="server" GroupName="sex" />女</td></tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr>
<td class="style1">班级:</td>
<td class="style2">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>B1</asp:ListItem>
<asp:ListItem>B2</asp:ListItem>
<asp:ListItem>B3</asp:ListItem>
<asp:ListItem>B4</asp:ListItem>
<asp:ListItem>B5</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr>
<td class="style1">爱好:</td>
<td class="style2">
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal" >
<asp:ListItem>足球</asp:ListItem>
<asp:ListItem>篮球</asp:ListItem>
<asp:ListItem>乒乓球</asp:ListItem>
<asp:ListItem>羽毛球</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr>
<td class="style1"> 照片:</td>
<td class="style2">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr>
<td class="style1">简介:</td>
<td class="style2">
<asp:TextBox ID="info" runat="server" TextMode="MultiLine" Columns="25"
Rows="5" ></asp:TextBox>
</td>
</tr>
<tr><td class="style1"> </td><td class="style2"> </td></tr>
<tr>
<td align="center" class="style1" colspan="3" >
<asp:Button ID="Button1" runat="server" Text="提交" OnClientClick="CleckInput();"
οnclick="Button1_Click" onprerender="Page_Load"/>
</td>
</tr>
<tr>
<td align="center" colspan="3" style="text-align:center"><asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
服务器端:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Data.SqlClient;
namespace studentinfo
{
public class dal
{
public string Connectrs = "Data Source=97E4E16433AA40F//SQLEXPRESS;Initial Catalog=StudInfo;Integrated Security=True";
public bool DoSql(string sql)
{
SqlConnection conn = new SqlConnection(Connectrs);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
if (cmd.ExecuteNonQuery() > 0)
{ return true; }
else
{ return false; }
conn.Close();
}
public DataTable DoTable(string sql)
{
SqlConnection conn = new SqlConnection();
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable();
adp.Fill(dt);
conn.Close();
return dt;
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
#region Get Data from client
string snum = this.num.Text;
string sname = this.username.Text;
string sps = this.password.Text;
string ssex = "";
if (this.xbnan.Checked == true)
{
ssex = "男";
}
else
{
ssex = "女";
}
string sclass = this.DropDownList1.SelectedValue.ToString();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected == true)
{
sb.Append(this.CheckBoxList1.Items[i].Text);
}
}
string shabby = "";
foreach (ListItem li in this.CheckBoxList1.Items)
{
if (li.Selected)
shabby+= li.Text;
}
string spicture = this.FileUpload1.PostedFile.FileName;
string sinfo = this.info.Text;
#endregion
#region validate the data from client
if (snum == "")
{
this.Label1.Text = "请输入学号!";
return;
}
else
{
if (sname == "")
{
this.Label1.Text = "请输入姓名!";
return;
}
else
{
if (sps == "")
{
this.Label1.Text = "请输入密码!";
return;
}
else
{
if (sb.ToString() == "")
{
this.Label1.Text = "请输入班级";
return;
}
else
{
if (!this.FileUpload1.HasFile)
{
this.Label1.Text = "请选择照片!";
return;
}
else
{
if (sinfo == "")
{
this.Label1.Text = "请输入简介!";
return;
}
}
}
}
}
}
#endregion
#region Save picture from client to server' hard disk
string oldfilename = this.FileUpload1.FileName;
string newfilename = System.DateTime.Now.ToString("yyyyMMddhhmmss") + "." + this.GetExtName(this.FileUpload1.FileName);
string filepath = MapPath("photos/" + newfilename);
this.FileUpload1.SaveAs(filepath);
#endregion
#region Save data from client to database
dal db = new dal();
string sql = "insert into std (snum,sname,spassword,ssex,sclass,shabby,spicture,sinfo) values ( '" + snum + "','" + sname + "','" + sps + "','" + ssex + "','" + sclass + "','" + shabby + "','" + spicture + "','" + sinfo + "')";
if (db.DoSql(sql))
{
Response.Write("<script language=javascript>alert('数据存储成功!');</script>");
this.clearInput();
}
else
{
Response.Write("<script language=javascript>alert('数据存储不成功!');</script>");
}
#endregion
}
private string GetExtName(string filename)
{
string[] s = filename.Split(new char[1] { '.' });
return s[s.Length - 1];
}
private void clearInput()
{
this.num.Text = "";
this.username.Text = "";
this.password.Text = "";
this.xbnan.Checked = false;
this.xbnv.Checked = false;
this.DropDownList1.SelectedIndex=0;
this.CheckBoxList1.SelectedIndex=0;
this.info.Text = "";
}
}
}