Rick van den Bosch - Blog

... on .NET, software architecture, software development and whatnot

Recent Posts

Tags

News

  • Live space

    Photo blog

    Follow me at twitter

    Rick  van den Bosch

    LinkedIn profile

    Add to Technorati Favorites

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Community

Email Notifications

Blogs I read

Interesting links

Archives

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

DotNetKicks.com said:

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

# April 10, 2008 12:41 PM

Scott said:

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...

# April 18, 2008 11:47 PM

jim said:

adding id? where is the id to be added?

can anyone please show me where linkbutton id to add?

# May 26, 2008 11:16 AM

zymen said:

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.

# May 26, 2008 12:07 PM

john said:

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

# June 17, 2008 3:16 AM

pavel said:

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!!!

# June 17, 2008 5:34 PM

Syed Shariq said:

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

# July 9, 2008 3:28 PM

sikaya said:

thanx!!

# August 25, 2008 3:12 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)