tags:

views:

4916

answers:

5

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?

+1  A: 

If the LinkButton is not inside some other thing (like a grid), just set the OnClientClick attribute in the code-behind like normal, using string.Format.

If it IS in a grid (or repeater, or something of that nature), set it in the RowDataBound event using FindControl or e.Row.Cells[] and string.Format. If you use e.Row.Cells[], you can probably dump the LinkButton server control and just output a normal anchor tag.

Robert C. Barth
It *is* is a repeater, and you gave me an idea that works. Set the OnClientClick to <%# GetClickClickText(Container.DataItem) %>
Ian Boyd
+2  A: 

Robert C. Barth gave me the idea that solves the problem:

<asp:LinkButton runat="server"
      OnClientClick="<%# GetItemClientClick((MySomething)Container.DataItem) %>" 
      Text="Do stuff" />

and then the code-behind file contains:

protected string GetItemClientClick(MySomething item)
{
   ...

   String szOnClientClick =
      "return DeleteItem(this, "+
            Toolkit.QuotedStr(item.NodeGUID.ToString()) + ", "+
            Toolkit.QuotedStr(GetItemText(item))+");";

   return szOnClientClick;
}

i really would have preferred to keep presentation in the aspx, and business logic in the code-behind - but the real world often doesn't conform to that model.

Ian Boyd
+5  A: 

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" />
Scott Ivey
i like this answer better. Sorry Robert.
Ian Boyd
A: 

i am new to C#, and i tried looking all the librabries for ToolKit class, can you tell which namespace contains ToolKit class?

Toolkit is our own class that contains useful helper funtions. In this case QuotedStr adds quotes around a string (and does some other useful stuff that need not be explained here). i could re-write the code sample to pretend QuotedStr is in the same class if you like.
Ian Boyd
+2  A: 

My Trick

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

Eval inside Eval

It Work for me !!

deadwalker82