webForum webForum sponsras med lina och serverplats av Binero AB

Gå tillbaka   webForum > Utveckling > Webbutveckling > .NET

.NET I detta forum diskuteras ämnen som berör .NET-plattformen, bland annat asp.net.

Svar
 
Trådverktyg Visningsalternativ
Äldre 2006-10-16, 15:23   #1
Travoni
Medlem
 
Registrerad: 2004-10-20
Ort: Ystad
Inlägg: 1 524
Lösningar: 37
Findcontrol() ifrån UC = nullExeption

Kod:
Partial Class custom_pager
    Inherits System.Web.UI.UserControl

    Public Sub Page_Load(ByVal obj As Object, ByVal e As EventArgs) Handles MyBase.Load

        Dim lbl As Label = CType(Page.FindControl("msg"), System.Web.UI.WebControls.Label)
        lbl.Text = "Raggadaggadooom!"

    End Sub

End Class
Jag får nullexeption när jag gör så hära ifrån min user control.

I verkligheten så hade jag tänkt att hämta controllens id med "me.parent.id" men bara som ett test så försökte jag mig på detta.
Hur gör jag?
__________________
Qui rogat, non errat
Travoni besöker inte forumet just nu   Svara med citat
Äldre 2006-10-16, 16:08 Markerad som löst av Travoni   #2
Travoni
Medlem
 
Registrerad: 2004-10-20
Ort: Ystad
Inlägg: 1 524
Lösningar: 37
mmm, jag var tvungen att köra en rekursiv funktion...
Kod:
Public Overloads Overrides Sub DataBind()
    Dim lbl As Label = CType(FindControlByID(Page.Controls, "msg"), Label)
    If TypeOf lbl Is Label Then
        lbl.Text = "Raggadaggadooom!"
    End If

    MyBase.DataBind
End Sub

Public Function FindControlByID(ByVal controls As ControlCollection, ByVal id As String) As Control
        Dim found As Control = Nothing
        For Each control As Control In controls
            If control.HasControls Then
                found = FindControlByID(control.Controls, id)
                If Not (found Is Nothing) Then
                    ' break 
                End If
            End If
            If control.ID = id Then
                found = control
                ' break 
            End If
        Next
        Return found
    End Function
__________________
Qui rogat, non errat

Senast redigerad av Travoni, 2006-10-16 klockan 18:48
Travoni besöker inte forumet just nu   Svara med citat
Äldre 2006-10-16, 22:44   #3
emission
Medlem
Kalebass
 
Registrerad: 1999-12-09
Ort: Norrköping
Inlägg: 6 095
Lösningar: 1232
Flytta upp kontrollen

" If control.ID = id Then"

ovanför den andra if-satsen, så lär prestanda bli rätt så mycket bättre, om du dessutom "return control" på en gång.

Man kan också tänka sig att använda vanliga FindControl inne i loopen, eftersom den internt arbetar mot en snabb Hashtabell.
emission besöker inte forumet just nu   Svara med citat
Äldre 2006-10-17, 11:16   #4
Travoni
Medlem
 
Registrerad: 2004-10-20
Ort: Ystad
Inlägg: 1 524
Lösningar: 37
Ok, jag hajjar, bra för en annan gång.
Jag tänkte göra en vanlig UC och använda som templateelement i en grid, men kom på att jag skall göra en iTemlate i stället, vilket är betydligt bättre för mitt ändamål.
__________________
Qui rogat, non errat
Travoni besöker inte forumet just nu   Svara med citat
Äldre 2007-01-14, 20:50   #5
petla81406
Medlem
 
Registrerad: 2004-05-09
Inlägg: 356
Lösningar: 5
Någon som snabbt bara kan skriva om funktionerna till C#?
petla81406 besöker inte forumet just nu   Svara med citat
Äldre 2007-05-15, 14:44   #6
Travoni
Medlem
 
Registrerad: 2004-10-20
Ort: Ystad
Inlägg: 1 524
Lösningar: 37
Bokmärk: http://www.developerfusion.co.uk/uti...btocsharp.aspx
Kod:
public override void DataBind() 
{ 
 Label lbl = ((Label)(FindControlByID(Page.Controls, "msg"))); 
 if (lbl is Label) { 
   lbl.Text = "Raggadaggadooom!"; 
 } 
 base.DataBind(); 
} 

public Control FindControlByID(ControlCollection controls, string id) 
{ 
 Control found = null; 
 foreach (Control control in controls) { 
   if (control.HasControls) { 
     found = FindControlByID(control.Controls, id); 
     if (!((found == null))) { 
     } 
   } 
   if (control.ID == id) { 
     found = control; 
   } 
 } 
 return found; 
}
__________________
Qui rogat, non errat
Travoni besöker inte forumet just nu   Svara med citat
Äldre 2007-05-15, 16:53   #7
emission
Medlem
Kalebass
 
Registrerad: 1999-12-09
Ort: Norrköping
Inlägg: 6 095
Lösningar: 1232
Men hellre:

Kod:
public override void DataBind() 
{ 
 Label lbl = FindControlByID(Page.Controls, "msg") as Label; 
 if (lbl!=null) { 
   lbl.Text = "Raggadaggadooom!"; 
 } 
 base.DataBind(); 
} 

public Control FindControlByID(ControlCollection controls, string id) 
{ 
 Control found = null; 
 foreach (Control control in controls) { 
   if (control.ID == id) { 
     return control; 
   }
   if (control.HasControls) { 
     found = FindControlByID(control.Controls, id); 
     if (found != null) {
        return found;
     } 
   } 
    
 } 
 return found; 
}

Senast redigerad av emission, 2007-05-15 klockan 17:06
emission besöker inte forumet just nu   Svara med citat
Äldre 2007-11-17, 13:56   #8
Addeladde
Medlem
 
Registrerad: 2001-01-11
Ort: Sthlm
Inlägg: 2 997
Lösningar: 4
Buggrättning
[kod]
public override void DataBind()
{
Label lbl = FindControlByID(Page.Controls, "msg") as Label;
if (lbl!=null) {
lbl.Text = "Raggadaggadooom!";
}
base.DataBind();
}

public Control FindControlByID(ControlCollection controls, string id)
{
Control found = null;
foreach (Control control in controls) {
if (control.ID == id) {
return control;
}
if (control.HasControls()) {
found = FindControlByID(control.Controls, id);
if (found != null) {
return found;
}
}

}
return found;
}
__________________
Andreas
Addeladde besöker inte forumet just nu   Svara med citat
Svar
webForum > Utveckling > Webbutveckling > .NET

Trådverktyg
Visningsalternativ

Forumregler
Du får inte posta nya trådar
Du får inte posta svar
Du får inte bifoga filer
Du får inte redigera dina inlägg

BB-kod är
Smilies är
[IMG]-kod är av
HTML-kod är av

Forumhopp


Alla tider är i GMT +1. Klockan är nu 19:22.


Powered by: vBulletin Version 3.8.6
Copyright © webForum