Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server"> <title>无标题页</title> <style type="text/css"> .style1 { font-size: small; } </style> </head> <body> <form id="form1" runat="server"> <div> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="style4" colspan="3"> <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/head.bmp" /> </td> </tr> <tr> <td align="right" class="style3"> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#" ImageUrl="~/Images/sc_03.gif">添加联系人信息</asp:HyperLink> </td> <td align="right" class="style1"> 用户编号:</td> <td class="style1"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="128px"> </asp:DropDownList> </td> </tr> <tr> <td align="right" class="style3"> <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="#" ImageUrl="~/Images/sc_10.gif">删除联系人信息</asp:HyperLink> </td> <td align="right" class="style1"> 姓名:</td> <td class="style1"> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="姓名不能为空!">*</asp:RequiredFieldValidator> </td> </tr> <tr> <td align="right" class="style3"> <asp:HyperLink ID="HyperLink4" runat="server" ImageUrl="~/Images/tj_05.gif" NavigateUrl="#">HyperLink</asp:HyperLink> </td> <td align="right" class="style1"> 昵称:</td> <td class="style1"> <asp:TextBox ID="txtNickName" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right" class="style3"> <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="#" ImageUrl="~/Images/sc_11.gif">退出登录</asp:HyperLink> </td> <td align="right" class="style1"> 性别:</td> <td class="style2"> <asp:RadioButtonList ID="radlistSex" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Selected="True">男</asp:ListItem> <asp:ListItem>女</asp:ListItem> </asp:RadioButtonList> </td> </tr> <tr> <td align="right" class="style3"> <asp:ValidationSummary ID="ValidationSummary1" runat="server" style="font-size: small" /> </td> <td align="right" class="style1"> 手机:</td> <td class="style1"> <asp:TextBox ID="txtphone" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtphone" ErrorMessage="您输入的号码有误,请重新输入" ValidationExpression="^[1]\d{10}">*</asp:RegularExpressionValidator> </td> </tr> <tr> <td align="right" class="style3"> </td> <td align="right" class="style1"> E-Mail:</td> <td class="style1"> <asp:TextBox ID="txtMail" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtMail" ErrorMessage="邮件格式不正确!" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator> </td> </tr> <tr> <td align="right" class="style3"> </td> <td align="right" class="style1"> 工作:</td> <td class="style1"> <asp:TextBox ID="txtWork" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="right" class="style3"> </td> <td align="right" class="style1"> 城市:</td> <td class="style1"> <asp:TextBox ID="txtCity" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style3"> </td> <td class="style1"> </td> <td class="style2" > <asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="~/Images/tj_07.gif" /> <asp:ImageButton ID="imgReg" runat="server" CausesValidation="False" ImageUrl="~/Images/cz_07.gif" /> </td> </tr> <tr> <td class="style5" colspan="3"> </td> </tr> </table> </div> </form> </body> </html>
Default.aspx.cs
View Code
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.Data.SqlClient;//引入命名空间 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string strUserID = "select * from AddLinkMen"; SqlConnection mycon = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); SqlCommand mycom = new SqlCommand(strUserID, mycon);//创建命令对象 if (mycon.State.Equals(ConnectionState.Closed)) { mycon.Open(); }//打开数据库连接 SqlDataReader sqr = mycom.ExecuteReader();//创建数据阅读器 if (sqr.HasRows)//判断数据库中是否有数据 { while (sqr.Read()) { DropDownList1.Items.Add(sqr["UserID"].ToString()); } ListItem li = new ListItem("请选择", "0"); DropDownList1.Items.Insert(0, li);//将联系人编号循环添加到下拉列表框中 } sqr.Close(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (DropDownList1.SelectedValue != null && DropDownList1.SelectedIndex != 0) { string cmdsql = "select * from AddLinkMen where UserID='" + Convert.ToInt32(DropDownList1.SelectedValue) + "'"; SqlConnection mycon = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]); SqlCommand mycom = new SqlCommand(cmdsql, mycon);//创建命令对象 if (mycon.State.Equals(ConnectionState.Closed)) { mycon.Open(); }//打开数据库连接 SqlDataReader myRead = mycom.ExecuteReader();//创建数据阅读器 if (myRead.HasRows)//判断是否有数据 { while (myRead.Read())//读取数据 { txtName.Text = myRead["UserName"].ToString(); txtNickName.Text = myRead["UserNickName"].ToString(); txtphone.Text = myRead["UserPhone"].ToString(); txtMail.Text = myRead["UserEmail"].ToString(); txtWork.Text = myRead["UserAdress"].ToString(); txtCity.Text = myRead["UserCity"].ToString(); } myRead.Close();//关闭数据阅读器 mycon.Close();//关闭数据库连接 } } else { txtName.Text = txtNickName.Text = txtphone.Text = txtMail.Text = txtWork.Text = txtCity.Text = ""; } } }