VS 在开发.net时怎么添加Page_load事件的代码

VS 在开发.net时怎么添加Page_load事件的代码,第1张

Vs中当你新建一个Aspx页面的时候,就会自动3个文件,XX.aspxXX.aspx.cs XX.aspx.designer.cs,其中designer.cs是系统自动生成的不用管,aspx.cs是页面后置类代码,你需要的page_load事件就在里面

PublicClassSimpleCalculatorInheritsSystem.Windows.Forms.Form#Region"Windows窗体设计器生成的代码"PublicSubNew()MyBase.New()'该调用是Windows窗体设计器所必需的。InitializeComponent()'在InitializeComponent()调用之后添加任何初始化EndSub'窗体重写处置以清理组件列表。ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)IfdisposingThenIfNot(componentsIsNothing)Thencomponents.Dispose()EndIfEndIfMyBase.Dispose(disposing)EndSub'Windows窗体设计器所必需的PrivatecomponentsAsSystem.ComponentModel.IContainer'注意:以下过程是Windows窗体设计器所必需的'可以使用Windows窗体设计器修改此过程。'不要使用代码编辑器修改它。FriendWithEventsLabel1AsSystem.Windows.Forms.LabelFriendWithEventsTextBox1AsSystem.Windows.Forms.TextBoxFriendWithEventsButton1AsSystem.Windows.Forms.ButtonFriendWithEventsButton2AsSystem.Windows.Forms.ButtonFriendWithEventsButton3AsSystem.Windows.Forms.ButtonFriendWithEventsButton4AsSystem.Windows.Forms.ButtonFriendWithEventsButton5AsSystem.Windows.Forms.ButtonFriendWithEventsButton6AsSystem.Windows.Forms.ButtonFriendWithEventsButton7AsSystem.Windows.Forms.ButtonFriendWithEventsButton8AsSystem.Windows.Forms.ButtonFriendWithEventsButton9AsSystem.Windows.Forms.ButtonFriendWithEventsButton10AsSystem.Windows.Forms.ButtonFriendWithEventsButton11AsSystem.Windows.Forms.ButtonFriendWithEventsButton12AsSystem.Windows.Forms.ButtonFriendWithEventsButton13AsSystem.Windows.Forms.ButtonFriendWithEventsButton14AsSystem.Windows.Forms.ButtonFriendWithEventsButton15AsSystem.Windows.Forms.ButtonFriendWithEventsButton16AsSystem.Windows.Forms.ButtonPrivateSubInitializeComponent()Me.Label1=NewSystem.Windows.Forms.LabelMe.TextBox1=NewSystem.Windows.Forms.TextBoxMe.Button1=NewSystem.Windows.Forms.ButtonMe.Button2=NewSystem.Windows.Forms.ButtonMe.Button3=NewSystem.Windows.Forms.ButtonMe.Button4=NewSystem.Windows.Forms.ButtonMe.Button5=NewSystem.Windows.Forms.ButtonMe.Button6=NewSystem.Windows.Forms.ButtonMe.Button7=NewSystem.Windows.Forms.ButtonMe.Button8=NewSystem.Windows.Forms.ButtonMe.Button9=NewSystem.Windows.Forms.ButtonMe.Button10=NewSystem.Windows.Forms.ButtonMe.Button11=NewSystem.Windows.Forms.ButtonMe.Button12=NewSystem.Windows.Forms.ButtonMe.Button13=NewSystem.Windows.Forms.ButtonMe.Button14=NewSystem.Windows.Forms.ButtonMe.Button15=NewSystem.Windows.Forms.ButtonMe.Button16=NewSystem.Windows.Forms.ButtonMe.SuspendLayout()''Label1'Me.Label1.Location=NewSystem.Drawing.Point(16,16)Me.Label1.Name="Label1"Me.Label1.Size=NewSystem.Drawing.Size(48,16)Me.Label1.TabIndex=0Me.Label1.Text="结果:"''TextBox1'Me.TextBox1.Location=NewSystem.Drawing.Point(64,8)Me.TextBox1.Name="TextBox1"Me.TextBox1.Size=NewSystem.Drawing.Size(120,21)Me.TextBox1.TabIndex=1Me.TextBox1.Text=""''Button1'Me.Button1.Location=NewSystem.Drawing.Point(200,8)Me.Button1.Name="Button1"Me.Button1.Size=NewSystem.Drawing.Size(56,24)Me.Button1.TabIndex=2Me.Button1.Text="清空"''Button2'Me.Button2.Location=NewSystem.Drawing.Point(48,56)Me.Button2.Name="Button2"Me.Button2.Size=NewSystem.Drawing.Size(24,24)Me.Button2.TabIndex=3Me.Button2.Text="1"''Button3'Me.Button3.Location=NewSystem.Drawing.Point(88,56)Me.Button3.Name="Button3"Me.Button3.Size=NewSystem.Drawing.Size(24,24)Me.Button3.TabIndex=4Me.Button3.Text="2"''Button4'Me.Button4.Location=NewSystem.Drawing.Point(136,56)Me.Button4.Name="Button4"Me.Button4.Size=NewSystem.Drawing.Size(24,24)Me.Button4.TabIndex=5Me.Button4.Text="3"''Button5'Me.Button5.Location=NewSystem.Drawing.Point(48,88)Me.Button5.Name="Button5"Me.Button5.Size=NewSystem.Drawing.Size(24,24)Me.Button5.TabIndex=6Me.Button5.Text="4"''Button6'Me.Button6.Location=NewSystem.Drawing.Point(88,88)Me.Button6.Name="Button6"Me.Button6.Size=NewSystem.Drawing.Size(24,24)Me.Button6.TabIndex=7Me.Button6.Text="5"''Button7'Me.Button7.Location=NewSystem.Drawing.Point(136,88)Me.Button7.Name="Button7"Me.Button7.Size=NewSystem.Drawing.Size(24,24)Me.Button7.TabIndex=8Me.Button7.Text="6"''Button8'Me.Button8.Location=NewSystem.Drawing.Point(48,120)Me.Button8.Name="Button8"Me.Button8.Size=NewSystem.Drawing.Size(24,24)Me.Button8.TabIndex=9Me.Button8.Text="7"''Button9'Me.Button9.Location=NewSystem.Drawing.Point(88,120)Me.Button9.Name="Button9"Me.Button9.Size=NewSystem.Drawing.Size(24,24)Me.Button9.TabIndex=10Me.Button9.Text="8"''Button10'Me.Button10.Location=NewSystem.Drawing.Point(136,120)Me.Button10.Name="Button10"Me.Button10.Size=NewSystem.Drawing.Size(24,24)Me.Button10.TabIndex=11Me.Button10.Text="9"''Button11'Me.Button11.Location=NewSystem.Drawing.Point(48,152)Me.Button11.Name="Button11"Me.Button11.Size=NewSystem.Drawing.Size(24,24)Me.Button11.TabIndex=12Me.Button11.Text="0"''Button12'Me.Button12.Location=NewSystem.Drawing.Point(176,56)Me.Button12.Name="Button12"Me.Button12.Size=NewSystem.Drawing.Size(24,24)Me.Button12.TabIndex=13Me.Button12.Text="+"''Button13'Me.Button13.Location=NewSystem.Drawing.Point(176,88)Me.Button13.Name="Button13"Me.Button13.Size=NewSystem.Drawing.Size(24,24)Me.Button13.TabIndex=14Me.Button13.Text="-"''Button14'Me.Button14.Location=NewSystem.Drawing.Point(176,120)Me.Button14.Name="Button14"Me.Button14.Size=NewSystem.Drawing.Size(24,24)Me.Button14.TabIndex=15Me.Button14.Text="*"''Button15'Me.Button15.Location=NewSystem.Drawing.Point(176,152)Me.Button15.Name="Button15"Me.Button15.Size=NewSystem.Drawing.Size(24,24)Me.Button15.TabIndex=16Me.Button15.Text="/"''Button16'Me.Button16.Location=NewSystem.Drawing.Point(88,152)Me.Button16.Name="Button16"Me.Button16.Size=NewSystem.Drawing.Size(72,24)Me.Button16.TabIndex=17Me.Button16.Text="计算"''SimpleCalculator'Me.AutoScaleBaseSize=NewSystem.Drawing.Size(6,14)Me.ClientSize=NewSystem.Drawing.Size(264,190)Me.Controls.Add(Me.Button16)Me.Controls.Add(Me.Button15)Me.Controls.Add(Me.Button14)Me.Controls.Add(Me.Button13)Me.Controls.Add(Me.Button12)Me.Controls.Add(Me.Button11)Me.Controls.Add(Me.Button10)Me.Controls.Add(Me.Button9)Me.Controls.Add(Me.Button8)Me.Controls.Add(Me.Button7)Me.Controls.Add(Me.Button6)Me.Controls.Add(Me.Button5)Me.Controls.Add(Me.Button4)Me.Controls.Add(Me.Button3)Me.Controls.Add(Me.Button2)Me.Controls.Add(Me.Button1)Me.Controls.Add(Me.TextBox1)Me.Controls.Add(Me.Label1)Me.Name="SimpleCalculator"Me.Text="简单计算器"Me.ResumeLayout(False)EndSub#EndRegionPrivateSubTextBox1_TextChanged(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)EndSubPrivateSubTextBox1_TabStopChanged(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)EndSubPrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.ClickDimbtnAsButton'按钮类型的变量btn=sender'把产生该事件的按钮对象赋值给btnTextBox1.Text=TextBox1.Text+btn.Text'把该按钮的Text属性值连接到TextBox1中EndSubPrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton4_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton4.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton5_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton5.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton6_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton6.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton7_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton7.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton8_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton8.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton9_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton9.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton10_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton10.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton11_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton11.ClickDimbtnAsButtonbtn=senderTextBox1.Text+=btn.TextEndSubPrivateSubButton12_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton12.ClickDimbtnAsButtonbtn=sender'在文本框的Text属性后连接一个空格、本按钮的Text属性值和一个空格TextBox1.Text=TextBox1.Text+""+btn.Text+""EndSubPrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.ClickTextBox1.Text=""'清空文本框EndSubPrivateSubButton13_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton13.ClickDimbtnAsButtonbtn=senderTextBox1.Text=TextBox1.Text+""+btn.Text+""EndSubPrivateSubButton14_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton14.ClickDimbtnAsButtonbtn=senderTextBox1.Text=TextBox1.Text+""+btn.Text+""EndSubPrivateSubButton15_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton15.ClickDimbtnAsButtonbtn=senderTextBox1.Text=TextBox1.Text+""+btn.Text+""EndSubPrivateSubButton16_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton16.Click'发生错误转移到标号“ErrorPro”指定的行去执行错误处理程序OnErrorGoToErrorProDimrAsDecimal'保存计算结果的变量DimtAsString=TextBox1.Text'用于保存文本框中的算术表达式DimspaceAsInteger=t.IndexOf("")'搜索空格位置,如果没有空格,返回值为0'字符串的取子符方法,第二个参数小于0,则将产生错误号为5的异常,即过程参数不正确Dims1AsString=t.Substring(0,space)'通过取子串方法获得第一个运算数DimopAsString=t.Substring(space+1,1)'通过取子串方法获得运算符Dims2AsString=t.Substring(space+3)'通过取子串方法获得第二个运算数Dimarg1,arg2AsIntegerarg1=Val(s1):arg2=Val(s2)SelectCaseopCase"+":r=arg1+arg2Case"-":r=arg1-arg2Case"*":r=arg1*arg2Case"/":r=arg1/arg2CaseElseMsgBox("输入的运算符有误!")ExitSubEndSelectTextBox1.Text=CStr(r)'显示结果ExitSub'退出过程ErrorPro:'错误处理程序块SelectCaseErr.NumberCase6'除数为零时,或运算溢出时的错误号MsgBox("算术运算溢出!",,"溢出提示")TextBox1.Focus()ExitSubCase5'Substring过程的参数不符合要求的错误号MsgBox("必须输入运算符和第二个运算数!",,"运算数少")ExitSubCaseElse'其它情况显示错误号和错误原因MsgBox("错误号为"&Err.Number&Chr(10)&Chr(13)&"错误原因:"&Err.Description)ExitSubEndSelectEndSubEndClass

里面建立一个表叫UsersTable,设计表,建立三个列:u_id, u_name, u_pwd。

3,u_id是int数据类型,自增1;u_name是nvarchar类型,长度12;u_pwd是varchar类型,长度12,然后保存该表。

4,打开表,在第一行u_name输入“abc”,u_pwd输入“abc”。关闭表。

5,打开VS,新建一个Web项目。双击默认的default.aspx页面,然后将 <body>... </body>替换成下面的代码:

HTML code

<!-- 替换-->

<body style="margin: 0px">

<form id="form1" runat="server">

<table border="0" cellpadding="0" cellspacing="0" style="width: 100%height: 50px">

<tr>

<td style="vertical-align: middlewidth: 50%text-align: centerfont-weight: boldfont-size: 25pxcolor: #003366font-family: Arial">yunleizhilian 的登录示范</td>

<td style="vertical-align: middletext-align: left">

<asp:Panel ID="pnlLogin" runat="server" Height="30px" Width="100%">

用户名:<asp:TextBox ID="txbUserName" runat="server"></asp:TextBox>密码:

<!-- 对于密码TextBox,应把TextMode属性设置为Password,这样输入的值会以****形式出现,保护密码在输入时不被别人看到 -->

<asp:TextBox ID="txbUserPassword" runat="server" TextMode="Password"></asp:TextBox>

<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" /></asp:Panel>

<asp:Panel ID="pnlWelcome" runat="server" Height="30px" Width="100%">

<asp:Label ID="lblWelcome" runat="server"></asp:Label>

<asp:Button ID="btnQuit" runat="server" CausesValidation="False" Text="退出" Width="146px" OnClick="btnQuit_Click" /></asp:Panel>

</td>

</tr>

</table>

<table border="0" cellpadding="0" cellspacing="0" style="width: 100%height: 100px">

<tr>

<td style="vertical-align: middletext-align: center">

<!-- 这个用来显示登录是否成功的信息 -->

<asp:Label ID="lblMessage" runat="server" ForeColor="#C00000"></asp:Label></td>

</tr>

</table>

</form>

</body>

<!-- 替换结束 -->

6,按F7,进入代码视图,在Page_Load()里写下面的代码:

C# code

protected void Page_Load(object sender, EventArgs e)

{

//确保页面是第一次被访问

if (!Page.IsPostBack)

{

CheckPageStatus()

}

}

//通过判断Session["UserName"]是否为空检查页面是否已登录(登录成功时会为Session["UserName"]注入值,就不空了。。否则就认为没有登录)。

private void CheckPageStatus()

{

if (Session["UserName"] != null)

{

pnlLogin.Visible = false

pnlWelcome.Visible = true

lblWelcome.Text = "欢迎登录," + Session["UserName"].ToString() + " 同志"

}

else

{

pnlWelcome.Visible = false

pnlLogin.Visible = true

}

//这个用来接收登录或退出后的信息。个人习惯,你也可以不这么做。

if (Session["Message"] != null)

{

lblMessage.Text = Session["Message"].ToString()

Session.Remove("Message")

}

else

lblMessage.Text = ""

}

7,先告一段落,按F5运行下,看看效果,你会发现当没有登录时,“退出”那部分没有显示。真神奇啊。

8,然后关闭这个IE页面,按VS左下角的“设计”,切换到设计视图。

9,双击设计视图的“登录”按钮,为它编写登录代码如下:

C# code

protected void btnLogin_Click(object sender, EventArgs e)

{

//下面的登录方法只是我的习惯而已,我常用DataTable做所有的事情。建议你不要依赖它……否则会被人笑话的……登录的好方法有很多,参考下就好了。

string strConnection = "SERVER=(local)DATABASE=UserTestUID=saPWD="//PWD=后面写上你的SQL SERVER 的 sa密码。

System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(strConnection)

//SQL 语句会吧?不会没办法,照抄吧。

string strSql = "SELECT * FROM UsersTable WHERE u_name='" + txbUserName.Text + "' AND u_pwd='" + txbUserPassword.Text + "'"

//SqlDataAdapter这东西有什么用,建议你去查MSDN。

System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(strSql, cn)

System.Data.DataTable dt = new DataTable()

da.Fill(dt)

if (dt.Rows.Count >0)

{

Session["UserName"] = dt.Rows[0]["u_name"].ToString()

Session["Message"] = "登录成功!"

Response.Redirect("default.aspx")

}

else

{

Session["Message"] = "登录失败。请重新登录。"

Response.Redirect("default.aspx")

}

}

10,再按F5测试下,在用户名和密码内输入123,按下“登录”按钮,会提示登录失败。如果都输入abc,就会提示登录成功。因为数据库内只有abc这个用户。

11,关闭IE页面,最后我们做退出。

12,回到设计视图,双击“退出”按钮,为它编写下面的代码:

C# code

protected void btnQuit_Click(object sender, EventArgs e)

{

if (Session["UserName"] != null)

{

Session.Remove("UserName")

Session["Message"] = "退出成功,欢迎您再来啊"

Response.Redirect("default.aspx")

}

}

13,按F5运行,登录成功后,按“退出”按钮,系统会提示已经退出。

14,为了验证用户是否已登录,你需要在所有需要登录的页面的Page_Load()内添加:

C# code

if(Session["UserName"]!=null)

//用户可以访问。

else

//用户不能访问,扔回到登录页面:Response.Redirect("login.aspx")

15,基本如此,搞定。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11539126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存