corso 2008

brani di codice

.NET

variabili di sistema

        Dim Item As String
        For Each Item In Request.ServerVariables
            Response.Write(Item & "-" & Request.ServerVariables(item) & "<br />")
        Next

variabili di sessione

        Response.Write(Session.Contents.Count() & & "<br />")
        Dim nome As String
        Dim i As Integer

        For Each nome In Session.Contents
            If IsArray(Session(nome)) Then
                For i = LBound(Session(nome)) To UBound(Session(nome))
                    Response.Write(nome & " - " & Session(nome)(i) & "<br />")
                Next
            Else
                Response.Write(nome & " - " & Session(nome) & "<br />")
            End If
        Next

conversioni

CType si usa per convertire classi, Convert per conversione di tipi.

Se in Session("user") abbiamo messo un oggetto di tipo riga, useremo CType:
lblUtente.Text = CType(Session("user"), Dati.UtentiRow).Ute_Nome

scrivere un file

Partial Class
	Imports System.IO
	dim sw as streamWriter
	sw = File.createText("c:\test.txt")
	sw.writeLine("ciao ciao")
	sw.Close()
End Class

login

Imports System.IO

Partial Class Login
    Inherits System.Web.UI.UserControl
     
    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
    	Handles btnLogin.Click
   
	Dim sr As System.IO.StreamReader
        Dim txt As String
        sr = System.IO.File.OpenText("C:\Login.txt")
        txt = sr.ReadLine()
        sr.Close()
        If txt = (txtUser.Text & txtPwd.Text) Then
            Session("User") = txtUser.Text
        Else
            ' errore
        End If
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) 
	Handles Me.PreRender
        If Session("User") Is Nothing Then
            pnlWelcome.Visible = False
            pnlLogin.Visible = True
        Else
            pnlWelcome.Visible = True
            pnlLogin.Visible = False
            lblutente.Text = Session("User").ToString
        End If
    End Sub
End Class


home