C# 如何将 Container.DataItem 作为参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/287358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you pass a Container.DataItem as a parameter?
提问by Brian Liang
I'm using a repeater control and I'm trying to pass a parameter as such:
我正在使用中继器控件,并且正在尝试传递这样的参数:
<%# SomeFunction( DataBinder.Eval(Container.DataItem, "Id") ) %>
It's basically calling:
它基本上是在调用:
public string SomeFunction(long id) {
return "Hello";
}
I'm not able to achieve this as I get an error:
由于出现错误,我无法实现此目的:
error CS1502: The best overloaded method match ... SomeFunction(long id) ... has some invalid arguments.
错误 CS1502:最佳重载方法匹配 ... SomeFunction(long id) ... 有一些无效参数。
Any ideas?
有任何想法吗?
采纳答案by Kieron
You need to cast the result to a long, so:
您需要将结果转换为 long,因此:
<%# SomeFunction( (long)DataBinder.Eval(Container.DataItem, "Id") ) %>
The alternative is to do something like this:
另一种方法是做这样的事情:
<%# SomeFunction(Container.DataItem) %>
and...
和...
public string SomeFunction(object dataItem) {
var typedDataItem = (TYPED_DATA_ITEM_TYPE)dataItem;
// DO STUFF HERE WITH THE TYPED DATA ITEM
return "Hello";
}
This at least allows you to work with multiple values from the data item (DataRows etc).
这至少允许您使用数据项(DataRows 等)中的多个值。
回答by wcm
I think you should cast the DataBinder.Eval(Container.DataItem, "Id") as long.
我认为您应该将 DataBinder.Eval(Container.DataItem, "Id") 投射尽可能长。
回答by Michael
I used this with success. The data source is a List collection.
我成功地使用了它。数据源是一个 List 集合。
OnClientClick='<%# "return myFunction(\""+ Container.DataItem + "\");" %>'
and the javascript function...
和 javascript 函数...
function myFunction(imgPath)
{
alert(imgPath);
}