i have a bit of aspx code that uses Eval to generate a call to a javascript function:
ASP.NET (wrapped for readability):
<asp:LinkButton runat='server' OnClientClick='<%# Eval( 'NodeGUID', 'return DoStuff(this, \'{0}\');') %>' Text='Do stuff' />
this generates javascript similar to:
Javascript (wrapped for readability):
return DoStuff(this, '3F2504E0-4F89-11D3-9A0C-0305E82C3301' );
Note: i’ve converted the generated " entities references into quotes for readability.
i now need to add a 3nd parameter to the javascript function call, a caption:
Javascript (wrapped for readability)
return DoStuff(this, '3F2504E0-4F89-11D3-9A0C-0305E82C3301', 'AllisonAngel.jpg' );
Note: i’ve converted the generated " entities references into quotes for readability.
There already exists a function in the code-behind file that is used to return the caption for an item:
C# (code omitted for readability):
protected string GetItemText(MySomething item) { ... }
i know that the above function can be called from the aspx file using a syntax similar to:
ASP.NET (wrapped, code omitted, for readability):
<asp:LinkButton ... runat='server' Text='<%# GetItemText((MySomething)Container.DataItem) %>' ... />
So now i want to use this function to include the 3rd parameter to the javascript function.
Starting from:
<asp:LinkButton runat='server' OnClientClick='<%# Eval( 'NodeGUID', 'return DoStuff(this, \'{0}\', \'Todo - Insert caption here\');') %>' Text='Do stuff' />
i need to change: ‘Todo - Insert caption here‘
into a call to: <%# GetItemText((MySomething)Container.DataItem) %>
Blindly trying the obvious:
ASP.NET (wrapped for readability):
<asp:LinkButton runat='server' OnClientClick='<%# Eval( 'NodeGUID', GetItemText((MySomething)Container.DataItem), 'return DoStuff(this, \'{0}\', \'{1}\');') %>' Text='Do stuff' />
But that complains, since Eval() only takes two parameters.
i tried the slightly less obivous:
ASP.NET (wrapped for readability)
<asp:LinkButton runat='server' OnClientClick='<%# Eval( 'NodeGUID', 'return DoStuff(this, \'{0}\', \''+GetItemText((MySomething)Container.DataItem)+'\');') %>' Text='Do stuff' />
But that doesn’t work either.
Related Questions
ASP.NET: How to access repeater generated elements from javascript?
The trick isn’t to pass multiple items to an eval, but to pass multiple eval’s to whatever you want to use to format the data. You could also have just done it this way – which would have kept the presentation in the aspx file like you wanted…