Confirm javascript on .NET Linkbutton in datagrids

Today I was struck with some really weird behaviour from a LinkButton in a Datagrid. I could not get the the confirm() javascript to fire. My code-behind was something like this:

LinkButton lb = ((LinkButton)dgItem.FindControl(”lb”));
lb.Attributes[”onClick”] = ”return confirm(’Do you really want to do that?’);”;

I dont really know why the onClick confirm-box does not fire before the href PostBack. However, the solution was to put the LinkButton inside a span and put the onClick attribute on the span.

// Web page
<span runat="server" id="spanLb">
<Linkbutton stuff/>
</span>

//Code-behind
HtmlControl spanLb = ((HtmlControl)dgItem.FindControl(”spanLb”));
spanLb.Attributes[”onClick”] = ”return confirm(’Do you really want to do that?’);”;

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *

Denna webbplats använder Akismet för att minska skräppost. Lär dig hur din kommentardata bearbetas.