Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Testi di due pagine ASP.NET

    ecco il codice di due pagine ASP.NET
    la prima consente di fare il login

    <%@ Page Language="C#" %>
    <script runat="server">

    void stampaSessione(Object sender, EventArgs e)
    {
    if (Session["nomeutente"] != "" )
    {
    Label1.Text = (string)Session["nomeutente"];
    }
    else
    {
    Label1.Text = "Utente mai visto prima";
    }
    }

    void login(Object sender, EventArgs e) {

    if (tbPassword.Value == "mavai")
    {
    Session["nomeutente"] = tbNomeutente.Value;
    Session["password"] = tbPassword.Value;
    Response.Redirect("success.aspx");
    }
    else
    {

    Response.Write("Nome utente non riconosciuto");
    }
    }

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">



    </p>



    <asp:Label id="Label1" runat="server" OnLoad="stampaSessione">Label</asp:Label>
    </p>



    Inserisci il nome utente : <input id="tbNomeutente" type="text" runat="server" />
    </p>



    Inserisci la tua password:
    <input id="tbPassword" type="password" runat="server" />





    <asp:Button id="Button1" onclick="login" runat="server" Text="Collegati"></asp:Button>
    </p>
    </form>
    </body>
    </html>


    la seconda denominata success.aspx dovrebbe visualizzare il messaggio di benvenuto personalizzato solo alla prima visita ma invece me lo fa vedere ogni volta anche se clicco su Refresh

    <%@ Page Language="VB" %>
    <script runat="server">

    sub Page_Load(Obj as Object, e as eventArgs)
    if not Page.IsPostBack then
    Dim adesso As DateTime = DateTime.Now
    label1.Text = "Bentornato " & Session("nomeutente")
    label2.Text = adesso.ToString("F")
    Response.Write ("
    ")

    Response.Write ("L'ID di sessione è " & Session.SessionID &"")
    end if

    end sub

    Sub Logout(sender As Object, e As EventArgs)
    Session("nomeutente").Abandon
    label1.Text = "Ti sei sconnesso correttamente"
    label2.Text = "alla prossima"
    Button1.visible = false
    End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">



    </p>



    <asp:Label id="Label1" runat="server">Label</asp:Label>
    </p>



    <asp:Label id="Label2" runat="server">Label</asp:Label>
    </p>



    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    </p>

    </form>
    </body>
    </html>

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    1,482
    Refresh e PostBack sono 2 cose diverse.

    Prova ad assegnare una Session quando l'utente si logga, quindi quando accede alla seconda pagina se la session è piena, mostra il messaggio e svuota la session stessa.

    Hey hey, my my Rock and roll can never die!

  3. #3
    Utente di HTML.it L'avatar di Mur
    Registrato dal
    Sep 2002
    Messaggi
    51
    cmq anchio ho un esempio molto simile su un manuale, è proprio il libro che non spiega benissimo la sua funzione...

  4. #4
    la stessa cosa l'ho preso da un manuale (o meglio questa che ho postato è la mia soluzione)
    questa è quella messa nel manuale

    1: <%@ Page Language=”C#” %>
    2:
    3: <script runat=”server”>
    4: void Submit_Click(object obj, EventArgs e) {
    5: if (tbPass.Value == “mypass”) {
    6: Session[“UserName”] = tbUser.Value;
    7: Response.Redirect(“exercise2.aspx”);
    8: } else {
    9: Label1.Text = “<font color=red>That “ +
    10: “is the wrong password!</font>”;
    11: }
    12: }
    13: </script>
    14:
    15: <html><body>
    16: <form runat=”server”>
    17:
    18: Please enter your username and password:


    19: <input type=”text” id=”tbUser”
    20: runat=”server”/>

    21: <input type=”password” id=”tbPass”
    22: runat=”server”/>
    23:
    24:


    25: <asp:Button id=”btSubmit”
    26: text=”Submit”
    27: runat=”server”
    28: OnClick=”Submit_Click” />


    29:
    30: <asp:Label id=”Label1” runat=”server”/>
    31: </form>
    32: </body></html>

    Il codice per exercise2.aspx è il seguente:
    1: <%@ Page Language=”VB” %>
    2:
    3: <script runat=”server”>
    4: sub Page_Load(obj as object, e as eventargs)
    5: if not Page.IsPostBack then
    6: Label1.Text = “Welcome “ & Session(“Username”) & _
    7: “!


    8:
    9: Label1.Text = Label1.Text & “The current time “ & _
    10: DateTime.Now.ToString("T") & "

    "
    11:
    12: Label1.Text = Label1.Text & “Your session id “ & _
    13: “is: “ & Session.SessionID & “


    14: end if
    15: end sub
    16:
    17: sub Submit_Click(obj as object, e as eventargs)
    18: Session.Abandon
    19: Label1.Text = “Your session has ended.”
    20: btSubmit.Visible = false
    21: end sub
    22: </script>
    23:
    24: <html><body>
    25: <form runat=”server”>
    26: <asp:Label id=”Label1” runat=”server”/>
    27:
    28: <asp:Button id=”btSubmit”
    29: text=”Submit”
    30: runat=”server”
    31: OnClick=”Submit_Click” />


    32: </form>
    33: </body></html>

  5. #5
    Utente di HTML.it L'avatar di Mur
    Registrato dal
    Sep 2002
    Messaggi
    51
    non è che sia sbagliato il codice del manuale, però non mostra molto bene l'utilizzo di ispostback

  6. #6
    esatto anche perchè a me il messaggio esce ogni volta e io volevo capire a che punto è che smetteva di essere visualizzato

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.