How do you pass a Container.DataItem as a parameter?

11,144

Solution 1

You need to cast the result to a long, so:

<%# 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).

Solution 2

I think you should cast the DataBinder.Eval(Container.DataItem, "Id") as long.

Solution 3

I used this with success. The data source is a List collection.

OnClientClick='<%# "return myFunction(\""+ Container.DataItem + "\");" %>'

and the javascript function...

function myFunction(imgPath)
{
    alert(imgPath);
}
Share:
11,144

Related videos on Youtube

Brian Liang
Author by

Brian Liang

iOS (Objective-C), C/C++, C#

Updated on April 18, 2022

Comments

  • Brian Liang
    Brian Liang about 2 years

    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.

    Any ideas?