tags:

views:

17

answers:

2

I have a ListView in which i have a function that creates images for users. I pass userGender,userImage1name,userIsImage1Aprooved values to the function in which i generate image. ie. if user has approved image i return it back, otherwise i return default image based on gender.

My question is, is there any way to avoid passing 3 parameters to that function and to pass whole DataRow so i can get values of columns i need?

In reality i pass about 12 parameters just made it easy for you.

ie. i want to achieve something like that <%# GetImage(Container.Item) %> while in GetImage() i would be able to access Item("some_column_name") or C# version Item["some_column_name"].

A: 

Yes, you can, just cast it to the type.

leppie
What the type would be if Container inside ListView? ListViewRowItem?
eugeneK
Have a look with the debugger :)
leppie
+1  A: 

Have you tried this? Because your code almost works (Container.DataItem should be used).

Create a method GetImage(object o) which returns a string to the image url. Convert the object to a DataRowView object to get all the values you want. for exampe:

public string GetImage(object o)
{
DataRowView dataRowView = o as DataRowView;
if (dataRowView == null)
    return "default_image.jpg";

// Do your big if statement here
if (dataRowView["column"] == "some values")
    return "image.jpg";
else
    return "default_image.jpg";
}

In the aspx use:

<%# GetImage(Container.DataItem) %>
Zenuka
How do i access this object and what should i cast it to?
eugeneK
See my edit (C#)
Zenuka
@Zenuka, thanks i did so and now i get an error "Index 1 is either negative or above rows count."
eugeneK
That has to do with the way you access your data in the DataRowView or somewhere else...
Zenuka