Javascript – asp.net: how to wire up LinkButton-like linking functionality with delegates on a table cell background

asp.netcdelegatesjavascriptuser-controls

I'm using ASP.NET user controls. I'm passing values to another user control via command arguments through a link button as follows:

asp:

LinkButton ID="ZoomPhotoLinkButton" CommandArgument='<%#(Eval("conid"))%>' CommandName="PassItemId" runat="server">Zoom It</asp:LinkButton>

What I really want is to make the entire background cell of my table clickable, so that a click anywhere on the table cell would pass the appropriate CommandName and CommandArgument and link appropriately.

Before I had this set up with delegates, I had the above behavior working as follows with simple JavaScript and inline code-behind functions passing URLs to inline JavaScript:

(I'm using square brackets rather than angle brackets because StackOverFlow's trying to parse my "table code"):

[td] onclick="window.location='<%# FormatDetailPageUrl((object)Eval("conid"))%>'" style='cursor:pointer;text-align: center; border:0px;'[/td]

I'm trying to do something functionally equivalent in terms of the click on the table cell, except to invoke the appropriate CommandName and CommandArgument vs. simple Javascript.

Thanks in advance.

Best Answer

ASP.Net uses the __doPostBack() function to do this magic. You can call it yourself; the first parameter is the eventTarget, second param is eventArgument.

So in your case, you can do the following:

<td onclick="__doPostBack('PassItemId', '<%#(Eval("conid"))%>')"></td>

You might also want to take a look at Page.GetPostBackEventReference

Related Topic