views:

731

answers:

3

Why does the following give me a compilation error for line B (Label2, outside UpdatePanel) but not for line A (Label1, inside UpdatePanel)? I would have expected both lines to give an error since both controls are within the same Repeater and should therefore not be directly accessible outside the repeater, since there is no one unique instance.

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Label1.ClientID;  // Line A - compiles fine
        Label2.Text = Label2.ClientID;  // Line B - "The name 'Label2' does not exist in the current context"
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:Repeater runat="server" ID="Repeater1">
                <ItemTemplate>
                    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
                        <ContentTemplate>
                            <asp:Label ID="Label1" runat="server" Text="Label1" />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                    <asp:Label ID="Label2" runat="server" Text="Label2" />
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>
+1  A: 

I'm betting if you comment out line B you'll get a run-time error on execution. Label1 is going to be a null reference.

When you create controls in the ASPX page Visual Studio tries to help you out by adding the controls to the code behind in the designer file which extends the class for the page. In this case it's adding one when it shouldn't be.

Short answer is it's a bug. You should submit it but it shouldn't be a blocking issue.

Spencer Ruport
No, there's no runtime exception either. I added code to Page_Load that bound the repeater to a list with two items, followed by Line A. The result was that the text for Label2 in the 2nd repeater item was modified.
Helen Toomik
A: 

Real question is why are you creating multiple update panels in a repeater ? Put one outside of the repeater and call it good. Or if you just want to refresh some text dont use an update panel, use a call back with some client side script to set the dom element. Check out this http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/

Mcbeev
A: 

Neither of those is proper anyway. You shouldn't be trying to directly reference a control that's contained within the ItemTemplate.

If you want to modify those Labels at runtime, you should be using OnItemDataBound and FindControl. To "find" the Label in the UpdatePanel, you'll need to use UpdatePanel.ContentTemplateContainer.FindControl().

Dave Ward
Thanks for not answering my question. As my original question clearly says, "I would have expected both lines to give an error", and I am wondering why this is not the case.
Helen Toomik
When you do things wrong, unpredictable results are predictable?
Dave Ward