.net 後台中對html標籤按鈕跳轉後台以及後台簡單驗證

來源:互聯網
上載者:User

標籤:

  

---------------------------------學霸,學神,大牛,hacker請繞道de分割線-----------------------------------------------------------------------

 

  嗯,這個可能比較簡單,原諒我這個學弱第一次做.net還查了好久的資料,所以貼出來,避免大家再走彎路了。

  因為html的button按鈕不是伺服器端控制項,所以得做如下改變才能跳轉到後台。

   <button type="submit"  runat="server" onserverclick="Login" >登入</button>    

前台 login.aspx的代碼

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="admin_index" %>        <!-- 這一行記得要變--> 2  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html lang="en" class="no-js"> 5  6     <head> 7  8         <meta charset="utf-8"> 9         <title>homepage 後台登入</title>10         <meta name="viewport" content="width=device-width, initial-scale=1.0">11         <meta name="description" content="">12         <meta name="author" content="">13 14         <!-- CSS -->15         <link rel="stylesheet" href="assets/css/reset.css">16         <link rel="stylesheet" href="assets/css/supersized.css">17         <link rel="stylesheet" href="assets/css/style.css">18 19         <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->20         <!--[if lt IE 9]>21             <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>22         <![endif]-->23 24     </head>25 26     <body>27 28         <div class="page-container">29             <h1>教師首頁後台登入</h1>30             <form id="form"  runat="server" method="post">31                 <input type="text" name="username"  placeholder="使用者名稱"/>32                 <input type="password" name="password"  placeholder="密碼"/>33                 <button type="submit"  runat="server" onserverclick="Login" >登入</button>             <!-- -->34                 <!-- 35                     <asp:Button ID="Button" runat="server"  Text="登  錄" OnClick="Login" />36                 //-->37                 <div class="error"><span>+</span></div>38             </form>39            40         </div>41         42         <!-- Javascript -->43         <script src="assets/js/jquery-1.8.2.min.js"></script>44         <script src="assets/js/supersized.3.2.7.min.js"></script>45         <script src="assets/js/supersized-init.js"></script>46         <script src="assets/js/scripts.js"></script>47 48     </body>49 50 </html>

   前台做好後,就是從後台去html標籤的值了,用Request.Form["username"]根據其name屬性去取值。然後如果使用者名稱密碼正確則跳轉頁面並在session中放入一個值,若不正確,彈出提示。

後台login.aspx.cs代碼

 1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.Security; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class admin_index : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12 13     }14     protected void Login(object sender, EventArgs e)15     {16         if (Request.Form["username"] == "123456" && Request.Form["password"] == "123456")17         {18             19             // FormsAuthentication.RedirectFromLoginPage(user, true);//使用.net的Security機制20             Session["login"] = "OK";                      //隨意在session中放入一個值21             Response.Redirect("~/admin/Paper.aspx");22            23         }24         else25         {26             Response.Write("<script>alert(‘請輸入正確的使用者和密碼!‘);</script>");27         }28 29 30     }31 }

下來就做後台驗證,就是如果沒有登入就不能做相關的操作。

後台Honor.asp.cs代碼(登入後隨意的一個頁面的後台)

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class Honor : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         if (Session["login"] == null || Session["login"].ToString() == "" && Session["login"].ToString() != "OK")13         {14 15             Response.Write("<script>alert(‘沒有登入,請登入‘);window.top.location.href=‘/Login.aspx‘;</script>");16 17 18         }19 20     }21 }

 上面通過對session中值的判斷來判斷使用者是否登入。

這個驗證方式比較簡單,本來樓主想使用.net的Security機制,可是不會,就只好用這個了,所以有會的同學可以教我一下嘛哈哈!

.net 後台中對html標籤按鈕跳轉後台以及後台簡單驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.