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

    [Asp.NET] Identificare componente che ha inviato il post back

    ciao!

    io questo pezzo di codice nel Page_load:
    codice:
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            If IsPostBack Then
                inviaRic()
            End If
        End Sub
    sarebbe possibile identificare qualce componente ha inviato il post back??

  2. #2
    Utente di HTML.it L'avatar di supermac
    Registrato dal
    Jun 2001
    Messaggi
    1,857
    trovato questo
    codice:
    public static Control GetPostBackControl(Page page)
    {
        Control control = null;
    
        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }
    l'else è abbastanza "discutibile"...
    W la Ferari effetrenavenave!
    il computer è un somaro veloce! (neanche tanto ndr)

  3. #3
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    ma non è sender, l'oggetto in questione ?

    (la butto sul facile, ma ricordo che anche io poi ci ho sbattuto non poco)

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Questo è codice fatto tanto tempo fa

    Comunque non l'ho mai usato perchè mi trovo meglio con la gestione degli eventi fatta da .net
    codice:
    Option Strict On
    Partial Class Chi_ha_effettuato_il_postback_postback
        Inherits System.Web.UI.Page
    
        Private ChiHaFattoPostBackID As String = ""
    
        Private Sub Page_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Label1.Text = ""
            If Me.IsPostBack Then
                If ChiHaFattoPostBackID = "" Then
                    If (Trim(Me.Request.Form("__EVENTTARGET")) <> "") Then
                        ChiHaFattoPostBackID = Trim(Me.Request.Form("__EVENTTARGET")) & " (__EVENTTARGET)"
                    End If
                End If
                'Label1.Text contiene l'id di chi ha fatto il postback
                Me.Label1.Text = ChiHaFattoPostBackID
    
            Else
    
            End If
    
        End Sub
    
        Public Overrides Sub RegisterRequiresRaiseEvent(control As System.Web.UI.IPostBackEventHandler)
            MyBase.RegisterRequiresRaiseEvent(control)
            Me.ChiHaFattoPostBackID = (DirectCast(control, WebControl).UniqueID) & " (RegisterRequiresRaiseEvent)"
        End Sub
    End Class
    Pietro

  5. #5
    ciao!

    scusate il ritardo.
    allora:
    codice:
       If IsPostBack Then
                Dim ctrlname As String = Me.Request.Form("__EVENTTARGET")
                Dim ctrl As Control = Me.FindControl(ctrlname)
                If ctrl IsNot Nothing AndAlso ctrl.ClientID = "btnUpRichiesta" Or ctrl.ClientID = "btnInviaRichiesta" Then
                    Diagnostics.Debug.WriteLine(ctrl.ClientID)
                End If
    
                'inviaRic()
            End If
    se clicco uno dei due tasti, tutto ok.
    se nella input text do invio dicendomi che ctrl era Nothing.
    però in teoria non l'ho eseguito il controllo?
    o ho sbagliato nella if??

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 © 2024 vBulletin Solutions, Inc. All rights reserved.