LinkButton in ListView in UpdatePanel causes full

2019-01-17 03:27发布

I have a LinkButton in a ListView in an UpdatePanel. I would like the button (well, any of them) to cause a partial postback, but they are causing a full page postback.

<asp:UpdatePanel ID="upOutcomes" UpdateMode="Conditional" runat="server">
  <ContentTemplate>
      <asp:ListView ID="lvTargets" runat="server" onitemdatabound="lvTargets_ItemDataBound">
        <ItemTemplate>
          <asp:LinkButton ID="lnkAddTarget" CssClass="lo" Text='<%# Eval("Title") + " <b>" + Eval("Level") + Eval("SubLevel") + "</b>" %>' runat="server"></asp:LinkButton>
        </ItemTemplate>
      </asp:ListView>
  </ContentTemplate>
</asp:UpdatePanel>

I found another post on stackoverflow which suggested adding this:

protected void lvTargets_ItemDataBound(object sender, ListViewItemEventArgs e) {
  var lb = e.Item.FindControl("lnkAddTarget") as LinkButton;
  tsm.RegisterAsyncPostBackControl(lb);  // ToolkitScriptManager
}

It hasn't made a difference...

There are a few other similar posts too, but I can't find a solution! Any ideas?

5条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-17 03:44

I resolved this problem by setting: ClientIDMode="AutoID" on the page directive of the applicable page like so:<%@ Page Title="" ClientIDMode="AutoID" Language="C#"%>, thus resolving my previous problem of having an ASP linkbutton within a ListView to cause a full postback.

However, this may require that any ASP control on the client code (Jquery, Javascript) be referred to by it's full name as it appears in the browser source code (I use Firebug in Firefox to get the names). For example, this Jquery function $("#ContentPlaceHolder1_btnCancelReferCustomer").click(function () { $("#divRefer").hide({ effect: "slide", duration: 200 }); return false; }); was changed to this (please note the asp button name change in selector): $("#ctl00_ContentPlaceHolder1_btnCancelReferCustomer").click(function () { $("#divRefer").hide({ effect: "slide", duration: 200 }); return false; });

查看更多
Lonely孤独者°
3楼-- · 2019-01-17 03:54

I resolved this problem by setting: ClientIDMode="AutoID" on the page directive of the applicable page like so:

<%@ Page Title="" ClientIDMode="AutoID" Language="C#"%>

This is working fine.

查看更多
Melony?
4楼-- · 2019-01-17 03:55

Try adding CommandName, CommandArgument attributes and the OnCommand event handler to your linkbutton like this:

<asp:LinkButton CommandName='test' CommandArgument='<%# Eval("Title") %>' ID="lnkAddTarget" runat="server" OnCommand="LinkButtonCommandEventHandler" />

Or - adding OnItemCommand handler to the whole ListView.

查看更多
唯我独甜
5楼-- · 2019-01-17 03:59

I resolved this problem by setting: ClientIDMode="AutoID" on the page directive of the applicable page like so:<%@ Page Title="" ClientIDMode="AutoID" Language="C#"%>

查看更多
做个烂人
6楼-- · 2019-01-17 04:06

The ClientIDMode setting in ASP.NET 4 lets you specify how ASP.NET generates the id attribute for HTML elements.

In previous versions of ASP.NET (i.e. pre 4), the default behavior was equivalent to the AutoID setting of ClientIDMode. However, the default setting is now Predictable.

Read Microsoft Article

AutoId is required for this because of the way the script manager expects the HTML controls to be generated in previous versions of .NET.

查看更多
登录 后发表回答