简体   繁体   中英

How do I access a string of a child object on aspx page?

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.

you might try something like

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

or

<%# DataBinder.Eval(Container.DataItem, "File.Url") %>

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM