Tuesday, May 3, 2011

asp.net: how do I wire up LinkButton-like linking functionality with delegates on a table cell background?

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

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 codehind 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 approprate CommandName and CommandArgument vs. simple Javascript.

Thanks in advance...

From stackoverflow
  • 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

    kendor : Thanks, Nicholas, this is definitely on the right track. The code that you provide above doesn't work exactly: I do get a postback, but my values don't appear to be being passed to load the detail page. It may be a consequence of the fact that the cells are in a user control, and that's tripping things up. I'll do more reading and see if I can't get this working. Thanks again.
    kendor : Another thing that may be tripping me up: I'm trying to invoke this behavior inside of a GridView template...
  • You can probably achieve this client-side, e.g. using jQuery.

    Have a look at this article, which does the same (with DIVs instead of TDs): http://newism.com.au/blog/post/58/bigtarget-js-increasing-the-size-of-clickable-targets/

0 comments:

Post a Comment