---
title: "Findcontrol() ifrån UC = nullExeption"
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/152575-findcontrol-ifrån-uc-nullexeption"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "Travoni"
published: "2006-10-16T14:23:45.000Z"
updated: "2007-11-17T13:56:02.000Z"
replies: 7
views: 969
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/152575-findcontrol-ifrån-uc-nullexeption"
---

# Findcontrol() ifrån UC = nullExeption

## #1 — Travoni, 2006-10-16T14:23Z

```
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?

Permalänk: https://www.webforum.nu/p/152575

## #2 — Travoni, 2006-10-16T15:08Z

mmm, jag var tvungen att köra en rekursiv funktion...

```
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
```

Permalänk: https://www.webforum.nu/p/1884076

## #3 — emission, 2006-10-16T21:44Z

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.

Permalänk: https://www.webforum.nu/p/1884224

## #4 — Travoni, 2006-10-17T10:16Z

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.

Permalänk: https://www.webforum.nu/p/1884331

## #5 — petla81406, 2007-01-14T20:50Z

Någon som snabbt bara kan skriva om funktionerna till C#?

Permalänk: https://www.webforum.nu/p/1923733

## #6 — Travoni, 2007-05-15T13:44Z

Bokmärk: <http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx>

```
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; 
}
```

Permalänk: https://www.webforum.nu/p/1977899

## #7 — emission, 2007-05-15T15:53Z

Men hellre:

```
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; 
}
```

Permalänk: https://www.webforum.nu/p/1977922

## #8 — Addeladde, 2007-11-17T13:56Z

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; 
}

Permalänk: https://www.webforum.nu/p/2052162

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/152575-findcontrol-ifrån-uc-nullexeption
