2

I'm trying to work with a class that has a child object, which has a string - and I'm trying to access this with in-line C# code on my aspx page.

More specifically, let's say I'm working with an object of 'Upload' class which has a Title property (String). An Upload object can also have a 'File' property (object). And each File object has a Url property (String).

I can access the Title like so:

<%# ((Upload)Container.DataItem)["Title"] %>

That works fine. But then how do I access a File's Url? Because the following does not work:

<%# ((File)((Upload)Container.DataItem)["File"]).Url %>

As you may be able to guess from the syntax, this is all within an asp repeater.

SkonJeet
  • 4,827
  • 4
  • 23
  • 32

3 Answers3

4

you might try something like

<%# Bind("File.Url") %>

or

<%# DataBinder.Eval(Container.DataItem, "File.Url") %>
JCherryhomes
  • 426
  • 4
  • 12
4

try this:

<%# ((Upload)Container.DataItem).File.Url %>

You get the container dataitem & cast it. Once you have the object, you can call it's properties & methods like any other object

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
1

I am just giving you a sample, you can implmement the same on your own:-

  1. First create a server side code to to return a URL of the File.
  2. Then call that function from client side to get the URL of the title passed to the same.

Below is an example which returns the text with the suffix dots

Step 1 : Create server side code to return text with suffix dots

public string ReturnDotSuffix(string strValue, int iFontSize, int iWidth)

{
    string strReturnValue = string.Empty;
    try
    {
        CommonLib objCommonLib = new CommonLib();
        strReturnValue = objCommonLib.SuffixDots(strValue, iFontSize, iWidth);

    }
    catch (Exception ex)
    {
        HandleException.ExceptionLogging(ex.Source, ex.Message, true);
    }
    return strReturnValue;
}

Step 2: Call this from Client Side.

Text='<%# ReturnDotSuffix((string)DataBinder.Eval(Container.DataItem, "MessageTitle"),8,170) %>'

The same can be done in your case.