【问题标题】:ASP.NET: "Object Required" when repeating LinkButtons in an UpdatePanelASP.NET:在 UpdatePanel 中重复 LinkBut​​tons 时“需要对象”
【发布时间】:2010-11-04 04:05:55
【问题描述】:

我有一个 UpdatePanel,它有一个重复链接按钮的中继器。当我单击 LinkBut​​ton 时,页面会进行部分回发,然后我收到一个 javascript 错误:“需要对象”。我尝试调试 javascript,但无法获得调用堆栈。如果我删除 UpdatePanel,LinkBut​​tons 会进行完整的回发,然后它们会从页面中消失。我怎样才能让这个 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());
    }
}

谢谢, 标记

【问题讨论】:

  • 我想我们需要看看绑定代码

标签: asp.net javascript updatepanel repeater


【解决方案1】:

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

如果我猜的话,您的问题是由于转发器没有在 PostBacks 中保持其 DataBound 状态。 Linkbutton 从视图中消失是因为它没有绑定到每个 PostBack 上的页面,所以当响应被发送回客户端时,它没有任何绑定。

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

尝试在其OnInit() 方法中将您的转发器绑定到页面/控件。这应该允许在每个 PostBack 上加载转发器的 ViewState。

【讨论】:

  • 即使我在 Page_Init 中进行绑定,仍然得到“需要对象”
猜你喜欢
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 2019-07-15
相关资源
最近更新 更多