本文實(shí)例為大家分享了.net后臺(tái)頁面統(tǒng)一驗(yàn)證是否登錄的具體代碼,供大家參考,具體內(nèi)容如下
首先新寫一個(gè)PageBase類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System; using System.Collections.Generic; using System.Web; namespace DepartmentMIS.Web.myclass { public class PageBase : System.Web.UI.Page { public PageBase() { this .Load += new EventHandler(BasePage_Load); } private void BasePage_Load( object sender, EventArgs e) { if (Session[ "UserNo" ] == null || Session[ "UserNo" ].ToString() == "" ) { Response.Redirect( "~/Login.aspx" ); } } } } |
Login頁面后臺(tái)部分代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
protected void btnLogin_Click( object sender, EventArgs e) { if (rblRole.SelectedValue == "1" ) { DataSet ds = AdminBLL.GetList( "userName = '" + tbxUserName.Text.Trim() + "' and password = '" + tbxPassword.Text.Trim()+ "' and isDeleted = 0" ); if (ds.Tables[0].Rows.Count == 1) { int id = Convert.ToInt32(ds.Tables[0].Rows[0][ "id" ]); Session[ "UserNo" ] = ds.Tables[0].Rows[0][ "id" ]; Session[ "UserName" ] = ds.Tables[0].Rows[0][ "userName" ]; Response.Redirect( "admin/adminIndex.aspx" ); } else { Response.Write( "<script>alert('用戶名或密碼錯(cuò)誤!')</script>" ); } } if (rblRole.SelectedValue == "2" ) { DataSet ds = StuBLL.GetList( "stuNo = '" + tbxUserName.Text.Trim() + "' and password = '" + tbxPassword.Text.Trim() + "' and isDeleted = 0" ); if (ds.Tables[0].Rows.Count == 1) { int id = Convert.ToInt32(ds.Tables[0].Rows[0][ "id" ]); Session[ "UserNo" ] = ds.Tables[0].Rows[0][ "id" ]; Session[ "UserName" ] = ds.Tables[0].Rows[0][ "stuName" ]; Response.Redirect( "student/stusIndex.aspx" ); } else { Response.Write( "<script>alert('用戶名或密碼錯(cuò)誤!')</script>" ); } } |
以stuWishChoices頁面為例,繼承PageBase類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using System.Web.UI.WebControls.WebParts; using System.Data.SqlClient; using System.Collections; namespace cbmis.ProDocumentMng { public partial class DocumentList : BasePage //繼承 { protected void Page_Load( object sender, EventArgs e) { } } } } |
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。