Reputation: 256591
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?
asp.NET: How to access repeater generated elements?
Upvotes: 13
Views: 32717
Reputation: 7350
you can try this
'<'asp:HyperLink ID="hp" runat="server" CommandArgument='<%# Eval("SysID", "{0}")%>'
NavigateUrl='<%# String.Format("~/ReportViewer.aspx?RevID= {0}
& User={1}", Eval("ID"), Eval("USER")) %>' Target="_blank" %>'>
</asp:HyperLink>
http://afzal-gujrat.blogspot.com/2012/10/pass-more-evals-in-querystring-with.html
Upvotes: 0
Reputation: 7439
Continued from Scott Ivey's solution, you can also use this to include if statements in your Eval statement:
For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.
<%# string.Format(Eval("Url") != null ? "<a href=\"{0}\">{1}</a>" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">
Upvotes: 0
Reputation: 41558
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" />
Upvotes: 21
Reputation: 31
My Trick
Eval("FLName", Eval("FLFDID","{0}")+"/{0}")
Eval inside Eval
It Work for me !!
Upvotes: 3
Reputation: 256591
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.
Upvotes: 2
Reputation: 23315
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.
Upvotes: 1