4

We have couple of lists in our application. Application types is a list which contains information about various applications - name, description and icon (as URL field with image format). Second one contains information about application instances. Second one contains information about schools name, address and so on. Application instances list has lookup field hooked up to ApplicationName field in types list and another lookup to SchoolName field in schools list.

What we are trying to do, is to get all items from application instances list, grouped by school. What we came up with now looks something like this:

    protected void LinqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
            {
                IEnumerable<SPListItem> apps = GetDataFromApplicationList();
                IEnumerable<SPListItem> types = GetDataFromApplicationTypeList();            

                e.Result = from app in apps
                           join type in types on app["Application name"] equals type.ID.ToString() + ";#" + type["ApplicationName"].ToString() into records  
                           from record in records
                           group record by app["SchoolLookup"] into Group
                           select new
                           {
                               Key = Group.Key,
                               Items = Group,
                           };
            }

In our view, we are using two nested listView components. First one is used to display group name, and second one is used to display items from that group. View code looks like this:

<asp:LinqDataSource runat="server" ID="LinqDS" OnSelecting="LinqDS_Selecting" />

(...)

<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDS"
OnItemCreated="ListView_ItemCreated">
  <LayoutTemplate>
    <table id="linki" cellpadding="0" cellspacing="0" border="0"
    style="text-align: left" width="100%">
      <tr class="head">
        <th class="first">Icon</th>
        <th>Name</th>
      </tr>
      <tr id="itemPlaceholder" runat="server" />
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr id="row" runat="server" class="group">
      <th colspan="5">Grupa: <%# Eval("Key")%>
      <br /></th>
      <th class="first"></th>
    </tr>
    <asp:ListView ID="ListView2" runat="server"
    DataSource='<%# Eval("Items")%>'
    OnSelectedIndexChanging="ListView2_SelectedIndexChanging"
    DataKeyNames="ID">
      <LayoutTemplate>
        <tr id="itemPlaceholder" runat="server" />
      </LayoutTemplate>
      <ItemTemplate>
        <tr id="row" runat="server" class="item hidden">
          <td class="frist">
            <asp:ImageButton ID="ImageButton1" runat="server"
            CommandName="SelectItem"
            OnCommand="ImageButton1_SelectItem"
            ImageUrl='<%# DataBinder.GetIndexedPropertyValue(Container.DataItem, "[\"Icon\"]")%>'
            CommandArgument='<%# Eval("ID")%>' />
          </td>
          <td>
            <%# DataBinder.GetIndexedPropertyValue(Container.DataItem, "[\"ApplicationName\"]")%>
          </td>
        </tr>
      </ItemTemplate>
    </asp:ListView>
  </ItemTemplate>
</asp:ListView>

We have two issues with this code. Both are in line nested list view's ImageButton tag. First issue is in CommandArgument attribute. We need to pass ID of Application (from application instances list) but code we have right now, we are passing ID of Application type. We don't know how to access ID of application instance in the Eval expression.

Second issue we have is in ImageUrl attribute. The way this attribute is evaluated right now, return image url and description separated by comma. We know that there something like SPFieldUrlValue, but how we eventually use this in data binder expression (if it is possible at all).

1 Answer 1

2

You can create a protected method in the code behind that receives an object, then cast that to the proper type (in this case SPListItem) and from there extract the values and create the appropriate types (such as SPFieldUrlValue, ImageFieldValue, and so on).
Same pattern can be easily followed to extract other kind of values from an object/item.

Example:

public static string EvalImageFieldValueUrl(object dataItem, string fieldName)
    {
        SPListItem item = (SPListItem)dataItem;
        if (item[fieldName] != null)
        {
            ImageFieldValue imageFieldValue = (ImageFieldValue)item[fieldName];
            return imageFieldValue.ImageUrl;
        }
        return "#";
    }

And then:

ImageUrl='<%# EvalImageFieldValueUrl(Container.DataItem, "Icon")%>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.