ASP.NET:“需要对象” 在 UpdatePanel 中重复 LinkBut​​ton 时

发布于 2024-07-25 03:40:09 字数 1896 浏览 4 评论 0原文

我有一个 UpdatePanel,其中有一个重复 LinkBut​​tons 的中继器。 当我单击 LinkBut​​ton 时,页面会进行部分回发,然后出现 JavaScript 错误:“需要对象”。 我尝试调试 JavaScript,但无法获取调用堆栈。 如果我删除 UpdatePanel,LinkBut​​ton 会执行完整的回发,并且它们会从页面中消失。 我怎样才能让这个 UpdatePanel 工作?

<ajax:UpdatePanel ID="wrapperUpdatePanel" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:Repeater ID="endpointRepeater" runat="server" OnItemDataBound="EndpointDataBound">
            <HeaderTemplate>
                <div class="sideTabs">
                    <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <asp:LinkButton ID="endpointLink" runat="server" OnClick="EndpointSelected" />
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
                </div>
            </FooterTemplate>
        </asp:Repeater>
    </ContentTemplate>
</ajax:UpdatePanel>

绑定代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.SelectedEndpoint = Factory.Get<IEndpoint>(Enums.EndPoints.Marketing);
    }
    IEndpointCollection col = EndpointCollection.GetActivelySubscribingEndpointsForPart(this.Item);

    if (this.Item.IsGdsnItem)
        col.Add(Factory.Get<IEndpoint>(Enums.EndPoints.Gdsn));

    if (col.Count > 0)
        col.Insert(0, Factory.Get<IEndpoint>(Enums.EndPoints.Marketing));

    this.endpointRepeater.DataSource = col;
    this.endpointRepeater.DataBind();

    if (this.endpointRepeater.Items.Count > 0)
    {
        LinkButton lb = this.endpointRepeater.Items[0].FindControl("endpointLink") as LinkButton;
        this.EndpointSelected(lb, new EventArgs());
    }
}

谢谢, 标记

I have an UpdatePanel which has a Repeater repeating LinkButtons. When I click a LinkButton, the page does a partial postback, then I get a javascript error: "Object required". I tried debugging the javascript, but couldn't get a call stack. If I remove the UpdatePanel, the LinkButtons do a full postback, and they disappear from the page. How can I get this UpdatePanel to work?

<ajax:UpdatePanel ID="wrapperUpdatePanel" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:Repeater ID="endpointRepeater" runat="server" OnItemDataBound="EndpointDataBound">
            <HeaderTemplate>
                <div class="sideTabs">
                    <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <asp:LinkButton ID="endpointLink" runat="server" OnClick="EndpointSelected" />
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
                </div>
            </FooterTemplate>
        </asp:Repeater>
    </ContentTemplate>
</ajax:UpdatePanel>

binding code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.SelectedEndpoint = Factory.Get<IEndpoint>(Enums.EndPoints.Marketing);
    }
    IEndpointCollection col = EndpointCollection.GetActivelySubscribingEndpointsForPart(this.Item);

    if (this.Item.IsGdsnItem)
        col.Add(Factory.Get<IEndpoint>(Enums.EndPoints.Gdsn));

    if (col.Count > 0)
        col.Insert(0, Factory.Get<IEndpoint>(Enums.EndPoints.Marketing));

    this.endpointRepeater.DataSource = col;
    this.endpointRepeater.DataBind();

    if (this.endpointRepeater.Items.Count > 0)
    {
        LinkButton lb = this.endpointRepeater.Items[0].FindControl("endpointLink") as LinkButton;
        this.EndpointSelected(lb, new EventArgs());
    }
}

thanks,
mark

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

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

发布评论

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

评论(1

爱情眠于流年 2024-08-01 03:40:09

这可能不是您的主要问题,但是当在 Repeater 中包含需要事件的对象时,您不应该使用该控件的本机事件。 相反,您应该使用中继器的 OnCommand 事件。

如果我猜测,您的问题是由转发器未在回发中维护其 DataBound 状态引起的。 Linkbutton 从视图中消失,因为它没有绑定到每个 PostBack 上的页面,因此当响应发送回客户端时,它没有任何绑定。

听起来 UpdatePanel 期望从 AJAX 响应中返回与页面上已有内容相同(或相似)的标记,因此为转发器返回任何内容都会导致问题。

尝试在其 OnInit() 方法中将中继器绑定到页面/控件。 这应该允许在每次回发时加载中继器的 ViewState。

This may not be your main issue, but when including an object inside a Repeater that needs an event, you shouldn't be using that control's native events. Instead you should use the Repeater's OnCommand event.

If I were to guess, your problem is caused by the repeater not maintaining its DataBound state across PostBacks. The Linkbutton disappears from view because it is not bound to the page on every PostBack, so when the response is sent back to the client, it has nothing bound to it.

It sounds like the UpdatePanel is expecting the same (or similar) markup to be returned from the AJAX response as what is on the page already, so returning nothing for the repeater causes problems.

Try binding your repeater to the page/control in its OnInit() method. This should allow the ViewState for the repeater to be loaded on every PostBack.

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