Mitchel Sellers
Mitchel Sellers

Reputation: 63136

Why does this cause Windows Authentication to appear and not an exception?

Ok, so after spending a good portion of a day debugging a stupid typing mistake inside a piece of code I am curious as to why the specific actions occured rather than an exception.

First of all the problem code.

Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
    Dim hl As New HyperLink
    AddHandler hl.DataBinding, AddressOf Me.BindData
    container.Controls.Add(container)
End Sub

The obvious problem is that we are trying to add the container to itself, which I would have expected to cause an exception. However, instead it caused the page to prompt the user for their login credentials (Windows authentication in the browser).

Does anyone have an idea why this is the case, and why an exception or something else didn't happen?

EDIT

The reason for the question is that due to this mistake, the page is rendered useless, and prompts for Windows Login, and NOT giving stack overflow exceptions or any other exception.

Upvotes: 1

Views: 168

Answers (2)

J Cooper
J Cooper

Reputation: 17062

Controls are something that can be arbitrarily nested; anything that is a Control can have a collection of other controls (I'm sure the reason for this is obvious). That being said, there's nothing "wrong" with adding a reference of a given object to a collection of such objects within itself. Though redundant, it's not exceptional.

Upvotes: 1

JaredPar
JaredPar

Reputation: 755249

Where would you expect the exception to happen? Inside this code there is no reason an exception should be generated. However illogical it may be to nest a control within a control it is still valid. It won't paint very well and unless the painting/drawing layer is specifically aware of nesting then that could cause a Stack Overflow/Infinite loop. But that wouldn't occur here, but during layout/paint.

Upvotes: 1

Related Questions