Ajax – ASP.NET: How to access repeater generated elements from javascript

ajaxasp.net

i have a series of rows that are generated using an asp:repeater:

<asp:repeater ID="itemsRepeater" 
      OnItemDataBound="itemsRepeater_ItemDataBound" 
      runat="Server">
   <itemtemplate>
      <tr>
         <td>
            <asp:HyperLink ID="linkView" runat="server"
               Text="<%# GetItemText((Item)Container.DataItem) %>" 
               NavigateUrl="<%# GetViewItemUrl((Item)Container.DataItem) %>" />
         </td>
         <td>
            <asp:HyperLink ID="linkDelete" runat="server"
                Text="Delete"
                NavigateUrl="<%# GetDeleteUrl((ActionItem)Container.DataItem) %>" />
         </td>
      </tr>
   </itemtemplate>
</asp:repeater>

The repeater creates an HTML table, with each row containing a link to an item and (what is essentially) a "Delete" link. The above simplified example code generates HTML similar to:

<TR>
<TD>
   <A href="ViewItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}">
      AllisonAngle_SoccerGirl001.jpg
   </A>
</TD>
<TD>
   <A href="DeleteItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}">Delete</A>
</TD>
</TR>

Now that all works, but i want to convert the "Delete" to client side. i want to be able click the link and it will, on the client javascript:

  • prompt an alert "Are you sure…"
  • have javascript issue server-hit to actually delete the item they want
  • remove the item from the client DOM tree

So there are four problems to be solved:

  1. How to hook up javascript to the client-side click of the Delete link.
  2. How to know what item the user clicked Delete
  3. Prevent a post-back
  4. Delete the row that the user clicked

That is my question.

From here on you will find my rambling attempts to solve it. Don't take anything below as relating in any way to any possible accepted solution. Just because i posted code below, doesn't mean any of it is useful. And it doesn't mean i'm anywhere within spitting distance of the best solution. And because i can't make anything below work – it must have gone down the wrong road.


My Attempts

Wiring Up Javascript

The first task is to convert the delete link HTML from something such as:

<A href="DeleteItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}">
   Delete
</A>

into something more javascripty:

<A href="#" 
      onclick="DeleteItem('DeleteItem.aspx?ItemGuid={19a149db-5675-4eee-835d-3d78372ca6f9}')">
   Delete
</A>

and the add the script:

<script type="text/javascript">
   //<![CDATA[
       function DeleteItem(deleteUrl)   
       {   
          //Ask the user if they really want to
          if (!confirm("Are you sure you want to delete INSERT NAME HERE?"))
          {
             return false;
          }

          //Call returns false if the server returned anything other than OK
          if (!DoAjaxHit(deleteUrl)
          {
             return false;
          }

          //Remove the table row from the browser
          var tableRow = document.getElementById("TODO-WHAT ID");
          if (row != null)
          {
             //TODO: how to delete the tableRow from the DOM tree?
             //document.Delete(tableRow) ?
          }

          //return false to prevent a postback
          return false;
       }
   //]]>
</script>

What combination of ASP code can be used to create the above? i hear that asp:LinkButton has an OnClientClick event, where you can wire up a javascript function:

<asp:LinkButton ID="linkDelete" runat="server"
   Text="Delete"
   OnClientClick="DeleteItem(<%# GetDeleteUrl((ActionItem)Container.DataItem) %>);"/>

Problem is that the rendered HTML is literally containing:

<a onclick="DeleteItem(&lt;%# GetDeleteUrl((ActionItem)Container.DataItem)) %>);" ...>
   Delete
</a>

If i change the client click event handler to:

   OnClientClick="DeleteItem('todo - figure this out');"/>

it works – as well as "todo – figure this out" can work.


Preventing Postbacks

The dummied down above javascript call actually happens (i can see my alert), but there's the next problem: Returning false from the javascript function doesn't prevent a postback. In fact, i can see that the href on the generated html code isn't "#", but rather

javascript:__doPostBack('ctl0....

i tried changing the ASPX code to include the OnClick handler myself:

   OnClick="#"
   OnClientClick="DeleteItem('todo - figure this out');"

But the compiler thinks the pound side is a pragma, and whines:

Preprocessor directives must appear as
the first non-whitespace character on
a line


Table Row Identity

The table rows don't have an ID, they're generated by the asp:repeater.

How can the javascript function know what triggered the click event? The javascript needs to be able to find the element, and remove it from the tree.

Note: i would of course prefer fade+collapse animation.

Normally you get an element by using

var tr = document.getElementById("the table row's id");

But the table rows don't have an easily knowable ID. Since there are multiple rows, the server generates the ID as it builds the table. i realize some solution is going to have to involve changing:

<TR>

into

<TR runat="server">

so that there will be server generated identity for each table row, but how do i reference the generated name from javsscript?

Normally i would have thought that the scripting problem would be solved by using multiple paramters:

function DeleteItem(tableRowElement, deleteUrl)
{
   //Do a web-hit of deleteUrl to delete the item


   //remove tableRowElement DOM object from the document tree
}

But the problem is just pushed elsewhere: How do you populate tableRowElement and deleteUrl for the call to the javascript function?


Such a simple problem: convert a click from a postback to client-side.

The volume of problems involved is getting quite idiotic. Which seems to indicate that either

  • the idea solution is something completely different
  • there is no solution

References

Stackoverflow: How do I fade a row out before postback?

Stackoverflow: Javascript before asp:ButtonField click

asp.net: Accessing repeater elements from javascript.

Stackoverflow: How to access repeater generated elements?

Best Answer

jQuery can dig out the tags for you:

$("[id$=linkDelete]").click(function() {
    DeleteItem(this.href);
});

That code says "find all the DOM elements whose ID ends with 'linkDelete' and wire up the following click event handler".