วันนี้กับ Asp.net

Filed under เรื่อยๆ

วันนี้ มีการเรียนเขียน Code ของภาษา Asp.net ฮะ เค้าว่าพรุ่งนี้จะสอบไม่รู้ผมจะทำได้หรือป่าวหนิ ฮ่าๆ ผมได้เอา Code มาให้ดูด้วยฮะ

นี่ครับเป็น Code ที่ผมใช้เขียน

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”Default.aspx.vb” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>

Keyname :
<asp:TextBox ID=”tbKeyName” runat=”server”></asp:TextBox>
<br />
Value :
<asp:TextBox ID=”tbValue” runat=”server”></asp:TextBox>
<br />
<asp:Button ID=”tbnAddNewSession” runat=”server” Text=”Add New Session” />
<asp:Button ID=”btnRemoveaSession” runat=”server” Text=”Remove a Session” />
<asp:Button ID=”btnRemoveallSession” runat=”server” Text=”Remove All Session” />
<asp:Button ID=”btnShowallSession” runat=”server” Text=”Show All Session” />
<br />
กำหนดอายุของ Session :
<asp:TextBox ID=”tbTimeOut” runat=”server”></asp:TextBox>
&nbsp;Minute(s)<asp:Button ID=”btnSessionTimeOut” runat=”server”
Text=”Set Session TimeOut” />
<br />
<asp:Label ID=”lbResult” runat=”server” BackColor=”White” ForeColor=”#3399FF”
Width=”90%”></asp:Label>
</div>
</form>
</body>
</html>

ส่วนในไฟล์ . ….aspx.vb จะเป็น Code แบบนี้ครับ

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub tbnAddNewSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbnAddNewSession.Click
If (tbKeyName.Text.Trim = “”) Then
lbResult.Text = “กรุณาใส่ KeyName ก่อน”
tbKeyName.Focus()
ElseIf (tbValue.Text.Trim = “”) Then
lbResult.Text = “กรุณาใส่ Value ก่อน”
tbValue.Focus()
Else
Session(tbKeyName.Text.Trim) = tbValue.Text.Trim
ShowAllSession()

End If
End Sub

Protected Sub btnRemoveaSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveaSession.Click
If (tbKeyName.Text.Trim = “”) Then
lbResult.Text = “กรุณาใส่ Keyname ก่อน”
tbKeyName.Focus()
Else
Session.Remove(tbKeyName.Text.Trim)
ShowAllSession()
End If
End Sub

Protected Sub btnRemoveallSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveallSession.Click
Session.RemoveAll()
ShowAllSession()
End Sub

Protected Sub btnSessionTimeOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSessionTimeOut.Click
Session.Timeout() = tbTimeOut.Text.Trim
ShowAllSession()
End Sub

Protected Sub btnShowallSession_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowallSession.Click
ShowAllSession()
End Sub
Sub ShowAllSession()
Dim i As Integer
Dim tmp As String
Dim Key As Object
tmp = “‘”
i = 0
tmp += “Session ID = ” & Session.SessionID & “<br/>”
tmp += “Session TimeOut = ” & Session.Timeout & ” นาที<br/>”
tmp += “มี Session ทั้งหมด = ” & Session.Count & ” Session(s)<br/>”
For Each Key In Session.Keys
tmp += “Session Number ” & i + 1 & ” : Session (” & Key & “) => Value = ” & Session(i) & ” <br/>”
i += 1
Next
lbResult.Text = tmp
End Sub
End Class

หวังว่าคงเอาไปใช้ประโยชน์กันได้นะครับ ^ ^

2 Comments

  1. Posted 16/06/2011 at 16:06 | Permalink

    สวัสดีครับ โห้เปลี่ยนไปเยอะเลยนะหน้าตาเว็บ ดูดีขึ้นเยอะเลยนะครับ ^_^

  2. sCRIPTPER™
    Posted 17/06/2011 at 16:44 | Permalink

    ขอบคุณมากครับ ^ ^

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*