LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

When you dynamically generate a LinkButton in code that will be placed inside an UpdatePanel, you'll experience that the Click event for the LinkButton results in a full postback in stead of the UpdatePanel being triggered. There's a rather simpel solution to this problem...

First, let's take a look at the scenario. Consider this HTML:
HTML_markup
There's a literal control outside of the UpdatePanel. There's a second literal control inside the UpdatePanel. Besides that, there is a PlaceHolder inside the UpdatePanel, where the generated LinkButton will be placed. The parent control doesn't have to be a PlaceHolder, it can also be a repeater or something like that.

Next, consider this code:
linkbutton_code
As you can see, a LinkButton is generated and added to the PlaceHolder in every PageLoad. In the same PageLoad, the literal outside of the UpdatePanel is updated. The literal inside the UpdatePanel is only updated when the LinkButton is clicked. This makes for something like this for the first time the page loads:
first_load 
and something like this after the LinkButton is clicked:
lbclicked_load 
Because the LinkButton is inside the UpdatePanel, you would think hitting it would trigger the UpdatePanel so only the literal control inside the UpdatePanel gets updated. The fact that the two literals have the same time on it in the 'after LinkButton clicked' image, shows that the entire page was posted back.
The problem here is in the fact that the ID for the LinkButton is not set in the code. Weird thing is that when you change the code to generate a normal ASP.NET Button (with or without an ID), the problem doesn't occur. This has something to do with the fact that both the input rendered for the button and the link rendered for the linkbutton have no ID, but the input control does have a name that can be used to relate events/postbacks to. The link doesn't have that either, so I'm guessing it has something to do with that.

So the solution is rather simple. Add a line in code where the ID for the control is set (like linkButton.ID = "TestLinkButton";) After clicking the linkbutton in my example, only the literal in the UpdatePanel is updated. Hooray! ;)
lbclicked2_load 


kick it on DotNetKicks.com

Technorati tags: ,,

Comments

# LinkButton inside UpdatePanel results in full postback

Thursday, April 10, 2008 12:41 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Friday, April 18, 2008 11:47 PM by Scott

Thank you very much for this article!!!

I just spent several hours pulling my hair out trying to figure out why 3 linkbuttons inside an UpdatePanel were causing full postbacks instead of just async posts. I added several new links and they all did async posts as expected. After reading your article I finally noticed the id's were missing on the 3 malfunctioning linkbuttons. Simply adding the id's fix the problem.

I owe you a beer...

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, May 26, 2008 11:16 AM by jim

adding id? where is the id to be added?

can anyone please show me where linkbutton id to add?

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, May 26, 2008 12:07 PM by zymen

Heh.. As Scott I did not set ID for my linkButton. Why? "Because it is autogenerated". Men, thanks for saving my time :) Second beer from me.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Tuesday, June 17, 2008 3:16 AM by john

Couldn't you have just made your updatepanel's conditional?

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Tuesday, June 17, 2008 5:34 PM by pavel

I worked on webpart for sharepoint that use AJAX and I was really angry why my code didn't work. You get me the answer. Thank you!!!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, July 09, 2008 3:28 PM by Syed Shariq

Use Image button instead of linkButton and use small pics as its caption. It will work fine and not result as full postback.

For I = 1 To 10

Dim imgBtnDel As New ImageButton

imgBtnDel.ImageUrl = "~/Buttons/Delete.jpg"

imgBtnDel.CommandName = "Delete"

imgBtnDel.CommandArgument = i

AddHandler imgBtnDel.Command, AddressOf Me.imgCommand

.............Controls.Add(ImageButton1)

Next I

Sub imgCommand(ByVal sender As Object, ByVal e As CommandEventArgs)

Label1.Text = e.CommandName & " - " & e.CommandArgument

End Sub

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, August 25, 2008 3:12 PM by sikaya

thanx!!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, September 15, 2008 12:10 PM by Azote

Thanks for a great article and for saving alot of more hours of work for me!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Friday, September 26, 2008 11:36 PM by Michael Paladino

Awesome!  You just solved a very frustrating problem for me.  Thanks!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Thursday, February 05, 2009 11:42 AM by Poldo

Thank you!!!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Friday, February 13, 2009 3:21 PM by Santhosh

Hi,

This did not solve my problem entirely, I don't have any click event associated with the link button, but itgenerated dynamnically and added to the placeholder which is in a updatepanel and has only clientsideclick to show a modalpopup extender on the page, but it results in fullpostback when I click on the link it just shows the modal and then does the postback eventually hiding the modal again as page gets reloaded!! crazy thing any idea why this is behavinig this way?

Regards,

Santhosh

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, March 16, 2009 11:56 AM by Abhilash

Nice tip.

Will keep in mind, while generating controls dynamically.

Thanks.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Thursday, May 14, 2009 5:08 AM by Aaron Clausen

Santhosh, I am experiencing exactly the same problem as you are. I'm using nyromodal. .Net is really frustrating and ruin the client side code to enable my nyromodal modal popups. I've been searching on this for almost 2 days and trying a million different things.

My conclusion is just about to be "UpdatePanels" are next to useless and I'm thinking of just coding everything directly using jQuery and web service calls. But this has its drawbacks too as you lose the state of the objects in the page and everything has to be static.

ASP.Net = EPIC FAIL. This would have been finished in 20 minutes with PHP.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Tuesday, June 09, 2009 9:51 AM by Arjan

Thanks.. solved my problem exactly!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, July 15, 2009 12:01 AM by NecroKorn

this option work, test!!!

<asp:UpdatePanel ID="UpdatePanel10" runat="server" UpdateMode="Conditional">

     <ContentTemplate>

         <td >

             <asp:LinkButton ID= "lnkTest" runat="server" CommandName="Sort"

CommandArgument="" Text="Test" OnCommand="Sort_lsvAvisos" />

         </td>

     </ContentTemplate>

     <Triggers>

         <asp:AsyncPostBackTrigger ControlID="lnkNAviso_lsvAvisos" EventName="Click" />

     </Triggers>

 </asp:UpdatePanel>

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Thursday, July 16, 2009 7:31 PM by Shane

At the risk of sounding redundant...

I OWE YOU A BEER!!!

I have been pulling my hair out for hours over this. Thanks a lot man.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, February 24, 2010 1:19 PM by Henrik Weimenhög

THANK YOU THANK YOU THANK YOU!!!

I have had this error a couple of months ago, and now my panel stopped working. This was the solution I was looking for!! THANK YOU!!!

So many hours wasted until I found this!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, June 09, 2010 12:02 AM by Mike

I was pulling my hair out too.  I had my LinkButtons being created inside a Repeater so didn't put IDs on them.  I was getting Buttons to work properly but not LinkButtons so I didn't even think that the ID was the problem.

You saved me today!  Thanks.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Friday, June 25, 2010 4:27 PM by Mark

You saved me tons of time.  Thanks!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, July 07, 2010 12:27 PM by Dexter

Thank you a lot!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, July 21, 2010 4:28 PM by Robert Brown

Thank you man.  This is exactly what I needed.  You have explained the inexplicable

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Tuesday, August 10, 2010 5:22 PM by Miklos

Thank you - solved my problem too - had a custom control with RenderChildren and once the IDs were set it worked as expected.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, October 04, 2010 8:25 PM by supasox

Thanks so much - adding an ID sorted the problem immediately!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, December 15, 2010 9:49 PM by Louis L.

Wow, what a time saver! Thank you so much for this post.

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Thursday, January 27, 2011 11:58 PM by Eric

I am also running into the same problem as Santhosh and Aaron mentioned. Have either of you figured a way to solve this problem or a work around?

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Monday, February 07, 2011 9:50 PM by NN

Thank you. I spent a lot of time researching this issue until I found this page. Thanks!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Tuesday, March 29, 2011 12:26 PM by Noelia

Unbelievable!! Very many thanks!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Saturday, May 21, 2011 7:18 PM by sushil

If you do not give id to linkbutton it will give compilation error.. So I dont get it which id u are talking about.. I m facing same problem and not able to solve it..

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, July 20, 2011 3:42 PM by Kris

Thanks a lot, I was rewriting a page I had previously made in VB.NET in VS2008 to C# in VS2010 when I ran into the same problem.

It didn't occur in VB in VS2008 though...

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Friday, December 30, 2011 11:03 PM by soso

Thank u very much ......

oky i have the same problem But …. i have buttons when i click on it i got some image buttons …… So i creat buttons and the place holder of the image in update panel …… and every thing work fine

Than i have another button2 with wich do other stff …

one i click in the button 2 ….

i loss all the image in the my first place holder ……

What to do ??

pleace !!!

# re: LinkButton inside UpdatePanel results in full postback, UpdatePanel not triggered

Wednesday, January 04, 2012 9:12 AM by Rick van den Bosch

@soso

You probably have these buttons inside the same updatepanel. Because the code in Button1 is not triggered when Button2 is clicked, you lose all the controls that were rendered by the code under Button1.

The solution might be to extract the generation of all the controls into a separate method, and call this method from both the Button1 and Button2 clicked event.

Hope this helps

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 5 and 6 and type the answer here: