Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple parameters to Eval()?

Tags:

asp.net

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 &quot; 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 &quot; 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?

asp.NET: How to access repeater generated elements?

like image 618
Ian Boyd Avatar asked Jan 26 '09 20:01

Ian Boyd


People also ask

How to pass multiple Eval value in JavaScript function?

HTML Markup The multiple Eval (DataBinder. Eval) values are passed as parameters to the ViewDetails JavaScript function using string. Format function. The string parameter values are passed within double quotes.

What is eval in asp net?

Eval is used to bind to an UI item that is setup to be read-only (eg: a label or a read-only text box), i.e., Eval is used for one way binding - for reading from a database into a UI field.


2 Answers

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...

<asp:LinkButton 
   runat="server" 
   OnClientClick='<%# string.Format(
          "return DoStuff(this, \"{0}\", \"{1}\");", 
          Eval("NodeGUID"), 
          GetItemText((MySomething)Container.DataItem)) %>' 
   Text="Do stuff" />
like image 58
Scott Ivey Avatar answered Oct 19 '22 15:10

Scott Ivey


My Trick

Eval("FLName", Eval("FLFDID","{0}")+"/{0}")

Eval inside Eval

It Work for me !!

like image 3
deadwalker82 Avatar answered Oct 19 '22 16:10

deadwalker82