Repeater 内的控制范围,带或不带 UpdatePanel

发布于 2024-07-14 01:50:57 字数 1378 浏览 7 评论 0原文

为什么以下代码会给我 B 行(Label2,UpdatePanel 外部)编译错误,但 A 行(Label1,UpdatePanel 内部)却没有编译错误? 我本希望这两行都会给出错误,因为两个控件都在同一个中继器内,因此不应在中继器外部直接访问,因为没有一个唯一的实例。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2024-07-21 01:50:57

我敢打赌,如果您注释掉 B 行,您将在执行时收到运行时错误。 Label1 将成为空引用。

当您在 ASPX 页面中创建控件时,Visual Studio 会尝试通过将控件添加到扩展页面类的设计器文件后面的代码来帮助您。 在这种情况下,它在不应该添加的时候添加了一个。

简短的回答是这是一个错误。 您应该提交它,但这不应该是一个阻塞问题。

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.

沙与沫 2024-07-21 01:50:57

真正的问题是为什么要在中继器中创建多个更新面板? 在中继器外面放一个就可以了。 或者,如果您只想刷新一些文本而不使用更新面板,请使用带有某些客户端脚本的回调来设置 dom 元素。 看看这个http://encosia.com/ 2007/07/11/为什么-aspnet-ajax-updatepanels-are-dangerous/

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/

苍景流年 2024-07-21 01:50:57

无论如何,这些都不合适。 您不应该尝试直接引用 ItemTemplate 中包含的控件。

如果您想在运行时修改这些标签,您应该使用 OnItemDataBound 和 FindControl。 要在 UpdatePanel 中“查找”标签,您需要使用 UpdatePanel.ContentTemplateContainer.FindControl()。

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文