当前位置:网站首页>asp. Net web page, ASP connects to the database, and uses asp:panel and asp:dropdownlist controls
asp. Net web page, ASP connects to the database, and uses asp:panel and asp:dropdownlist controls
2022-06-26 03:59:00 【march of Time】
A recent course used asp.net, I have no choice but to learn its code , Here's a paragraph asp.net Login code :
Login.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs" Inherits="login" %>
<asp:Panel ID="Panel1" runat="server">
<table width="214" border="0" cellpadding="0" cellspacing="0" class="text">
<TR>
<TD>
<IMG height=57 src="images/denglu.jpg" width="214" border="0">
</TD>
</TR>
<tr>
<td align=center>
<table width=203px bgcolor=white cellpadding=0 cellspacing=0 class="text1">
<tr>
<td align=right width=30%> identity :</td>
<td align=left width=70%>
<asp:DropDownList ID="UserType" runat="server" Width="120">
<asp:ListItem Value=" Student "> Student </asp:ListItem>
<asp:ListItem Value=" Teachers' "> Teachers' </asp:ListItem>
<asp:ListItem Value=" Management "> Management </asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td align=right> user name :</td>
<td align=left>
<asp:TextBox ID="UserId" Runat="server" Width="120" /><font color="red" class="Normal">*</font>
<asp:RequiredFieldValidator id="RFVUserId" runat="server" ErrorMessage="<br> The username cannot be empty ." ControlToValidate="UserId" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right> password :</td>
<td align=left>
<asp:TextBox ID="Password" Runat="server" CssClass="InputText" Width="120" TextMode="Password" /><font color="red">*</font>
<asp:RequiredFieldValidator id="RFVPassword" runat="server" ErrorMessage="<br> The password cannot be empty ." ControlToValidate="Password" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right> Verification Code :</td>
<td align=left>
<asp:TextBox ID="Validator" Runat="server" Width="70"></asp:TextBox><font color="red">*</font>
<asp:Image ID="ValidateImage" runat="server" Height="25px" Width="50px" ImageAlign="AbsBottom" />
<asp:RequiredFieldValidator id="rfv" runat="server" ErrorMessage="<br> Verification code cannot be empty ." ControlToValidate="Validator" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="LoginBtn" Runat="server" Text=" Sign in " CssClass="ButtonCss" Width="70px" OnClick="LoginBtn_Click"></asp:Button>
</td>
</tr>
<tr>
<td colspan="2" align="center"><asp:Label ID="Message" Runat="server" CssClass="GbText" Width="100%" ForeColor="Red"></asp:Label></td>
</tr>
</table>
</td>
</tr>
<tr><td>
<IMG height=12 src="images/42.jpg" border="0">
</td></tr>
</table>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Height="50px" Width="100%">
<table width="214" border="0" class="text">
<tr>
<td align=left height=48 background="images/shuzhuo.jpg">
</td>
</tr>
<tr>
<td align=center>
<table width="203" bgcolor=white border="0" cellpadding=0 cellspacing=0 class="text">
<tr>
<td align=left>
Login as :<font class=text1>
<%=Session["UserType"]%></font>
</td>
</tr>
<tr>
<td align=left>
User name :
<font class=text1>
<%=Session["UserName"]%></font>
</td>
</tr>
<tr>
<td>
<hr />
</td>
</tr>
</table>
</td>
<tr><td>
<IMG height=12 src="images/42.jpg" width=214 border=0>
</td></tr>
</table>
</asp:Panel>
Login.aspx.cs
public partial class login : System.Web.UI.UserControl
{
private static string sValidator = ""; // Validation string
private readonly string sValidatorImageUrl = "ValidateImage.aspx?Validator=";
protected void Page_Load(object sender, EventArgs e)
{
// Check whether the system is logged in , Determine which Panel
if (Session["UserID"] != null)
{
// Already logged in , Show “ My desk ”
this.Panel1.Visible = false;
this.Panel2.Visible = true;
}
else
{
// Not logged in
this.Panel2.Visible = false;
this.Panel1.Visible = true;
// If the page is loaded for the first time , Create a verification code
if (!Page.IsPostBack)
{
// Create validation string
sValidator = CreateValidateString(4);
// Set the... Of the verification code image ImageUrl attribute
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
}
// Give a default user type .0- Student ;1- teacher ;2- Management
this.UserType.SelectedIndex = 2;
}
}
// Create validation string
private string CreateValidateString(int nLen)
{
// Create a StringBuilder object
StringBuilder sb = new StringBuilder(nLen);
Random rnd = new Random();
int rndi = 1000 + rnd.Next(8999);
sb.Append(rndi.ToString());
return (sb.ToString());
}
// User presses “ Sign in ” Button to verify
protected void LoginBtn_Click(object sender, EventArgs e)
{
// If the page input is illegal
if (! Page.IsValid)return;
// If the verification code is entered incorrectly
if (Validator.Text != sValidator)
{
Message.Text = " Error in captcha input , Please re-enter .";
// Recreate the validation string
sValidator = CreateValidateString(4);
// Reset the verification code picture ImageUrl attribute
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
return;
}
String userId = "";
String userName = "";
// Define the class and get the login information of the user
User user = new User();
// Encode user input
//string sUserType = Server.HtmlEncode(UserType.SelectedItem.Value.ToString());
string sUserType = Request.Params["Login1$UserType"].ToString();
string sUserId = Server.HtmlEncode(UserId.Text.Trim());
string sPassword = Server.HtmlEncode(Password.Text.Trim());
// Get user information
SqlDataReader dr = user.GetUserLoginBySQL(sUserType, sUserId, sPassword);
// Judge whether the user is legal
if (dr.Read())
{
userId = dr["UserID"].ToString();
userName = dr["UserName"].ToString();
}
dr.Close();
// Verify user legitimacy , And jump to the system platform
if ((userId != null) && (userId != ""))
{
// Write session object
Session["UserType"] = sUserType;
Session["UserID"] = userId;
Session["UserName"] = userName;
// Write Cookies
Response.Cookies["userInfo"]["UserType"] = sUserType;
Response.Cookies["userInfo"]["UserID"] = userId;
Response.Cookies["userInfo"]["UserName"] = userName;
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
// Jump to the system home page after login
//Response.Redirect("~/mainframe.aspx");
Response.Write("<script>window.open('mainframe.aspx','_top');</script>");
}
else
{
// Create validation string
sValidator = CreateValidateString(4);
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
// Display error message
Message.Text = " Wrong user name or password .";
}
}
}
Connect to database :
using System;
using System.Data;
using System.Configuration; // Read Web.config
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; // In order to connect SQL Server database
//using System.Data.OracleClient; // In order to connect Oracle database , You also need to add references
using System.Data.OleDb; // In order to connect OleDb database
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = "";
try
{
switch (DropDownList1.Text.ToString())
{
case "SQL Server":
connectionString = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
SqlConnection SQLConn = new SqlConnection(connectionString);
SQLConn.Open();
Response.Write(" Link status :" + (SQLConn.State == ConnectionState.Open ? " Link successful ." : " link failure .") + "<br>");
Response.Write(" Link string :" + SQLConn.ConnectionString + "<br>");
Response.Write("ConnectionTimeout:" + SQLConn.ConnectionTimeout.ToString() + "<br>");
Response.Write("DataSource:" + SQLConn.DataSource + "<br>");
Response.Write("ServerVersion:" + SQLConn.ServerVersion + "<br>");
SQLConn.Close();
break;
case "Access":
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
OleDbConnection AccessConn = new OleDbConnection(connectionString);
AccessConn.Open();
Response.Write(" Link status :" + (AccessConn.State == ConnectionState.Open ? " Link successful ." : " link failure .") + "<br>");
Response.Write(" Link string :" + AccessConn.ConnectionString + "<br>");
Response.Write("ConnectionTimeout:" + AccessConn.ConnectionTimeout.ToString() + "<br>");
Response.Write("DataSource:" + AccessConn.DataSource + "<br>");
Response.Write("ServerVersion:" + AccessConn.ServerVersion + "<br>");
AccessConn.Close();
break;
default :
Response.Write("Please select a kind of Database.<br>");
break;
}
}
catch (Exception ex)
{
/// Display the message of connection error
Response.Write(ex.Message + "<br>");
}
}
}
边栏推荐
猜你喜欢

Machine learning notes - trend components of time series

使用SOAPUI访问对应的esb工程

神经网络学习小记录71——Tensorflow2 使用Google Colab进行深度学习

MapReduce execution principle record

Flask入门
![[Flink] Flink source code analysis - creation of jobgraph in batch mode](/img/8e/1190eec23169a4d2a06e1b03154d4f.jpg)
[Flink] Flink source code analysis - creation of jobgraph in batch mode

2021 year end summary

商城风格也可以很多变,DIY 了解一下

Open Camera异常分析(一)

What preparation should I make before learning SCM?
随机推荐
Analysis of updatechild principle of widget update mechanism of fluent
But the Internet began to have a new evolution and began to appear in a new state
Three level menu applet
开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP
Machine learning notes - trend components of time series
链路监控 pinpoint
DETR3D 多2d图片3D检测框架
使用SOAPUI访问对应的esb工程
2022.6.24-----leetcode. five hundred and fifteen
(15)Blender源码分析之闪屏窗口显示菜单功能
Webrtc series - 7-ice supplement of network transmission preference and priority
I/O 虚拟化技术 — VFIO
商城风格也可以很多变,DIY 了解一下
What's wrong with connecting MySQL database with eclipse and then the words in the figure appear
Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
EF core Basics
XML parsing bean tool class
Slide the menu of uni app custom components left and right and click switch to select and display in the middle
User control custom DependencyProperty
捕获数据包(Wireshark)