i have a repeater than creates a table:
<itemtemplate> <tr id='theTableRow' runat='server'> <td> <asp:LinkButton runat='server' OnClientClick='todo' Text='Do Something' /> </td> </tr> </itemtemplate>
Note: the OnClientClick='todo' line.
In the final rendered code, i want the todo to contain a call to a javascript function, passing:
- the ID of the generated table row, and
- the Eval of a property of the currently bound object
And now for some pseudo-code:
Pseudocode 1:
OnClientClick=’DoSomething(theTableRow, CromulentGuid); return false;’
Pseudocode 2
OnClientClick=’javascript:DoSomething(theTableRow, CromulentGuid); return false;’
Pseudocode 3
OnClientClick=’javascript:DoSomething(theTableRow, <%# Eval(‘CromulentGuid’) %>); return false;’
Pseudocode 4
OnClientClick=’javascript:DoSomething(<%= theTableRow %>, <%# Eval(‘CromulentGuid’) %>); return false;’
Pseudocode 5
OnClientClick=’javascript:DoSomething(<%= Eval(theTableRow) %>, <%# Eval(‘CromulentGuid’) %>); return false;’
Whatever the ASP.NET code used, i want the rendered HTML to be:
<tr id='ctl00__itemRepeater_ctl01_theTableRow'> <td> <a onclick='DoSomething('ctl00__itemRepeater_ctl01_theTableRow', '19a149db-5675-4eee-835d-3d78372ca6f9'); return false;' href='javascript:__doPostBack('ctl00$itemRepeater$ctl01$ctl04','')'> Do Something </a> </td> </tr>
i would also be okay with:
<tr id='ctl00__itemRepeater_ctl01_theTableRow'> <td> <a onclick='DoSomething("ctl00__itemRepeater_ctl01_theTableRow", "19a149db-5675-4eee-835d-3d78372ca6f9"); return false;' href='javascript:__doPostBack('ctl00$itemRepeater$ctl01$ctl04','')'> Do Something </a> </td> </tr>
Note: i’m okay with the 2nd form since i know it is functionally identical, and ASP.NET code cannot generate the former, even if the latter is less readable.
Related questions:
ASP.NET: How to access repeater generated elements from javascript?
You can use the
OnItemDataBoundevent to alter each element in your code. Since you’re particular about the HTML I might also recommend using hybrid controls rather than asp controls. For example:This probably isn’t 100% perfect, as I just typed it directly in the reply window and I always screw up the Eval() syntax on my first go, but it should help some.