Wednesday, April 21, 2010

While loading controls inside an asp:LinkButton will render correctly on Page_Load, after post back the control loses the contained controls

e.g.
<asp:LinkButton id="btnShowReport" runat="server" OnClick="btnShowReport_Click">
<img src="images/16doc.gif" height="16" width="16" />
<asp:Label id="lblReportTitle" runat="server"/>
</asp:LinkButton>

This code appeared within an asp:Repeater, where the CommandName and CommandArgument of the LinkButton were being set dynamically along with the Text property of the Label. But on postback to bthShowReport_Click, all the links essentially disappeared. I discovered that the <a> tags where still in the page on PostBack, but the contents making them functional where gone.

It was a nice try but now... Just as simple but perhaps more elegant, but mostly IT WORKS!

<asp:ImageButton id="btnShowReport" runat="server" OnClick="btnShowReport_Click" ImageUrl="images/16doc.gif" style="vertical-align: middle"/>
<a>
<asp:Label id="lblReportTitle" runat="server" AssociatedControlID="btnShowReport"/>
</a>

I use the associated control to tie the label to the click event of the image button and some anchor tags give the linkbutton feel to the label.
Could have made the label a linkbutton and use the same onclick event handler as the imagebutton . But in the method I would have to check the type to do the correct cast of sender in order to get the command name and argument.

Dumb problem, simple solution. It just drove me nuts to see all the links disappear on postback so had to blog it.

Monday, April 19, 2010

Born in the West Indies, but for the next 2 weeks I'm a PROUD CANADIAN!

GO CANADA!
http://picasaweb.google.com/trinijessel See my photos from the 2010 Winter Olympics here in Vancouver