1、請創建一個簡單的網站。要求:
WebForm1.aspx頁面爲用戶登陸頁面,登陸成功的判斷條件爲:可登陸成功的用戶名爲:computer Application15,密碼爲:2015Comp。清空按鈕功能用於將用戶名和密碼清空,關閉按鈕用於關閉該網頁。請編寫出該網站的前端界面和後臺代碼(用C#語言進行編寫)。
aspx代碼(前端界面的代碼):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style>
table,td{
border:solid 1px #00ffff;
}
</style>
</head>
<body>
<form id="form1" runat="server" >
<div>
<table>
<tr>
<td>用戶名</td>
<td colspan="3">
<asp:TextBox id="text" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>密 碼</td>
<td colspan="3">
<asp:TextBox id="password" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td colspan="3">
<asp:Button ID="Button1" runat="server" Text="登錄" OnClick="Button1_Click1" />
<asp:Button ID="Button2" runat="server" Text="清空" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="關閉" OnClick="Button3_Click" />
</td>
</tr>
</table>
</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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//點擊關閉按鈕的時候關閉當前頁面
//服務器數據返回時,生成頁面時,關閉掉生成的頁面
this.Button3.Attributes.Add("onclick", "window.close();");
}
protected void Button1_Click1(object sender, EventArgs e)
{
string Name = this.text.Text;
string password = this.password.Text;
if (Name == "computer Application15" && password == "2015Comp")
{
Response.Write("<script>alert('登錄成功!!')</script>");
}
else
{
//Response.Write表示服務器端向頁面寫數據
Response.Write("<script>alert('用戶名或密碼輸入錯誤')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
this.text.Text = "";
this.password.Text = "";
}
//同時數據提交到後臺
protected void Button3_Click(object sender, EventArgs e)
{
//onclick是通過javascript執行的,是頁面端程序,此代碼不會提交到服務器端
//所以服務器端返回信息時,此代碼銷燬了,返回時不會提交到客戶端,所以無法關閉頁面。
//this.btn3.Attributes.Add("onclick", "window.close();");//兩者是同時觸發的,
}
}
點擊保存,然後就是運行該代碼,點擊相應的按鈕就會觸發相應的點擊事件!!!