如何将多个参数传递给Eval()?

时间:2021-07-24 16:44:17

i have a bit of aspx code that uses Eval to generate a call to a javascript function:

我有一些aspx代码,使用Eval生成对javascript函数的调用:

ASP.NET (wrapped for readability):

ASP。可读性净(包装):

<asp:LinkButton runat="server"
   OnClientClick='<%# Eval(
         "NodeGUID", 
         "return DoStuff(this, \"{0}\");") %>'
   Text="Do stuff" />

this generates javascript similar to:

生成的javascript类似于:

Javascript (wrapped for readability):

Javascript可读性(包装):

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函数调用中添加一个第3个参数,一个标题:

Javascript (wrapped for readability)

Javascript可读性(包装)

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):

c#(可读性代码省略):

protected string GetItemText(MySomething item)
{
   ...
}

i know that the above function can be called from the aspx file using a syntax similar to:

我知道上述函数可以从aspx文件中调用,其语法类似于:

ASP.NET (wrapped, code omitted, for readability):

ASP。NET(封装,代码省略,便于阅读):

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

现在我想使用这个函数将第3个参数包含到javascript函数中。

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"

我需要更改:“Todo -插入标题”

into a call to: <%# GetItemText((MySomething)Container.DataItem) %>

调用:<%# GetItemText((MySomething)容器. dataitem) %>。

Blindly trying the obvious:

盲目的努力显而易见的:

ASP.NET (wrapped for readability):

ASP。可读性净(包装):

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

但是这是有问题的,因为Eval()只接受两个参数。


i tried the slightly less obivous:

我尝试了稍微不那么明显的

ASP.NET (wrapped for readability)

ASP。净可读性(包装)

<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:如何从javascript访问中继器生成的元素?

asp.NET: How to access repeater generated elements?

asp。NET:如何访问中继器生成的元素?

6 个解决方案

#1


19  

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

诀窍不是将多个项传递给eval,而是将多个eval传递给您想要用来格式化数据的任何东西。您也可以这样做——这样就可以像您希望的那样将表示保存在aspx文件中……

<asp:LinkButton 
   runat="server" 
   OnClientClick='<%# string.Format(
          "return DoStuff(this, \"{0}\", \"{1}\");", 
          Eval("NodeGUID"), 
          GetItemText((MySomething)Container.DataItem)) %>' 
   Text="Do stuff" />

#2


3  

My Trick

我的技巧

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

Eval(“FLName Eval(“FLFDID”、“{ 0 }”)+ " / { 0 } ")

Eval inside Eval

Eval Eval内

It Work for me !!

这对我很有效!!

#3


2  

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

Robert C. Barth给了我一个解决问题的想法:

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

我确实希望在aspx中保持表示,在代码后保持业务逻辑——但是现实世界通常不符合这个模型。

#4


1  

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.

如果LinkButton不在其他东西(比如网格)中,就像普通的一样,在代码背后设置OnClientClick属性,使用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.

如果它在网格中(或中继器,或类似的东西),请使用FindControl或e.Row在RowDataBound事件中设置它。细胞[]和string.Format。如果你使用e.Row。单元格[],您可以转储LinkButton服务器控件,并输出一个普通的锚标记。

#5


0  

Continued from Scott Ivey's solution, you can also use this to include if statements in your Eval statement:

根据Scott Ivey的解决方案,您还可以使用这个来在Eval语句中包含if语句:

For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.

例如,如果属性Url存在,下面将输出一个锚标记,否则它将输出一个span。

<%# string.Format(Eval("Url") != null ? "<a href=\"{0}\">{1}</a>" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">

#6


0  

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

http://afzal-gujrat.blogspot.com/2012/10/pass-more-evals-in-querystring-with.html

#1


19  

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

诀窍不是将多个项传递给eval,而是将多个eval传递给您想要用来格式化数据的任何东西。您也可以这样做——这样就可以像您希望的那样将表示保存在aspx文件中……

<asp:LinkButton 
   runat="server" 
   OnClientClick='<%# string.Format(
          "return DoStuff(this, \"{0}\", \"{1}\");", 
          Eval("NodeGUID"), 
          GetItemText((MySomething)Container.DataItem)) %>' 
   Text="Do stuff" />

#2


3  

My Trick

我的技巧

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

Eval(“FLName Eval(“FLFDID”、“{ 0 }”)+ " / { 0 } ")

Eval inside Eval

Eval Eval内

It Work for me !!

这对我很有效!!

#3


2  

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

Robert C. Barth给了我一个解决问题的想法:

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

我确实希望在aspx中保持表示,在代码后保持业务逻辑——但是现实世界通常不符合这个模型。

#4


1  

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.

如果LinkButton不在其他东西(比如网格)中,就像普通的一样,在代码背后设置OnClientClick属性,使用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.

如果它在网格中(或中继器,或类似的东西),请使用FindControl或e.Row在RowDataBound事件中设置它。细胞[]和string.Format。如果你使用e.Row。单元格[],您可以转储LinkButton服务器控件,并输出一个普通的锚标记。

#5


0  

Continued from Scott Ivey's solution, you can also use this to include if statements in your Eval statement:

根据Scott Ivey的解决方案,您还可以使用这个来在Eval语句中包含if语句:

For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.

例如,如果属性Url存在,下面将输出一个锚标记,否则它将输出一个span。

<%# string.Format(Eval("Url") != null ? "<a href=\"{0}\">{1}</a>" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">

#6


0  

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

http://afzal-gujrat.blogspot.com/2012/10/pass-more-evals-in-querystring-with.html