28

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?

James
  • 7,343
  • 9
  • 46
  • 82
  • Just tried it, thanks. No luck though. :( I need it to update conditionally anyway. – James Apr 13 '11 at 17:02
  • Try populating the collection with the controls that will update the panel, even though the control is within the panel. – Brian Mains Apr 13 '11 at 17:20
  • How would I do that? 'lnkAddTarget' is not visible outside the ListView. Thanks. – James Apr 13 '11 at 17:26
  • 5
    I ended up needing to set ClientIDMode="AutoID" in the Page directive. No idea why - but it worked! I hope this helps the next person who gets stuck with this. – James Apr 13 '11 at 21:37
  • 2
    I suggest you add your last comment as an "answer", so future searchers will find it easier:) – Alex from Jitbit Apr 14 '11 at 11:22
  • 1
    i encountered the same problem, i make it working without `AutoID` by using `OnItemCreated` instead of `OnItemDataBound`. Apparently if you use the second one the UpdatePanel only works for only one asyn-postback! the second try will always cause full post-back, which i have no idea why... – GtEx Apr 20 '13 at 20:09
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="partlist.aspx.cs" Inherits="partlist" ClientIDMode="AutoID" enableEventValidation="true" %> ClientIDMode="AutoID" - because of this the update panel for listview is working perfect – sanjay Apr 24 '18 at 14:38

5 Answers5

38

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.

Steve Parker
  • 766
  • 6
  • 4
  • 1
    Thanks. This drove me crazy for half a day. As far I tried, I had to put a ID to the linkbutton and put AutoID in the page directive. – Larry May 27 '11 at 08:11
  • 1
    half a day? I've been working on this for over a week.. i thought for sure it was my UpdatePanel or some other wacky control I was using. Turns out it was just my linkbutton.. gaah... – Rob Dec 12 '11 at 19:56
  • 1
    Thank you my good man. I switched from a DataList to a ListView and couldnt figure out why my UpdatePanel stopped working suddenly – Dogoku Aug 24 '12 at 07:26
  • 4
    Thank you so much! Added ClientIDMode="AutoID" in the as an attribute, fixed! +1 – Lukas Jul 02 '13 at 19:50
1

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.

L_J
  • 2,351
  • 10
  • 23
  • 28
0

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; });

0

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

Salman Taj
  • 46
  • 3
0

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.

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149