Hvordan kan jeg se hvilken control postback henvender sig til?
Jeg har en side med en masse usercontrols og når der trykkes på en knap vil jeg gerne i en vilkårlig usercontrols page_load se om postbacken er til den aktuelle control eller til en anden.
Dvs.
Usercontrol1.asxc.vb ---------------------- Private Sub Page_Load.... If not ispostback then If postback/event is to this control then .... end if
Jeg bruger denne lille snippet til at finde ud hvilken kontrol der bevirkede postback: Function GetPostbackControl(ByVal targPage As Page) As Control If targPage.IsPostBack Then ' try to find the name of the postback control in the hidden ' __EVENTTARGET field Dim ctlName As String = targPage.Request.Form("__EVENTTARGET") ' if the string is not null, return the control with that name If ctlName.Trim().Length > 0 Then Return targPage.FindControl(ctlName) End If ' the trick above does not work if the postback is caused by standard ' buttons. ' In that case we retrieve the control the ASP-way: by looking in the ' Page's Form collection ' to find the name of a button control, that actually is the control ' that submitted the page Dim keyName As String For Each keyName In targPage.Request.Form Dim ctl As Control = targPage.FindControl(keyName) ' if a control named as this key exists, ' check whether it is a button - if it is, return it! If Not ctl Is Nothing Then If TypeOf ctl Is Button Then Return ctl End If End If Next End If Return Nothing End Function
Er Page variablen som du sender med ind nu også det Page objekt som kontrollerne er på? Hvis du har metoden til at ligge på din side så skal den vel kaldes med Me: Response.write(GetPostbackControl(Me).ID.ToString)
Det ser ikke ud til at brianv's link løser problemet.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.