Clickable labels

Discussion in 'ASP .Net Web Controls' started by Peter Kirk, Nov 2, 2005.

  1. Peter Kirk

    Peter Kirk Guest


    how do I create a "link" which calls a method in my code behind?

    I have a repeater which shows a table of results, and some of the cells in
    the table need to be clickable and cause things to happen.

    Could be something like the following I want (doesn't work of course). Here
    I try to create a cell with a clickable label which calls the method
    MyMethod in my code behind, passing the id of the clicked row.

    <asp:repeater id="ResultsRepeater" Runat="server">
    <asp:Label ID="voyagelabel" Runat="server"
    text="<%# ((DataRow)Container.DataItem).Name %>">
    onclick="MyMethod(<%# ((DataRow)Container.DataItem).Id

    Peter Kirk, Nov 2, 2005
    1. Advertisements

  2. I think you should use a LinkButton control instead of trying to reinvent
    the wheel by hacking the label control.
    Steve C. Orr [MVP, MCSD], Nov 3, 2005
    1. Advertisements

  3. Peter Kirk

    Peter Kirk Guest

    Yes, I eventually came to the same realisation. However, I can't get the
    link button (which is created dynamically in the repeater) to fire a call to
    the "click" method in the code behind....
    Peter Kirk, Nov 3, 2005
  4. Peter Kirk

    Demetri Guest

    Use the Repeater's ItemCommand event. When the linkbutton is clicked the
    ItemCommand event of the repeater will fire and pass event args specifying
    what CommandName is passed via the LinkButton's CommandName attribute.

    Hope that makes sense, otherwise check out ItemCommand on MSDN for the
    Repeater control.
    Demetri, Jan 5, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.