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?
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.
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.
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" />
My Trick
Eval("FLName", Eval("FLFDID","{0}")+"/{0}")
Eval inside Eval
It Work for me !!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With