1

I'll probably answer my own question before I finish this post, but in case I fail here what I'm grappling with.

I need to get the PhotoId value out of Club:ImageThumbnail control:

<asp:FormView ID="fvPhpto" runat="server" DataKeyNames="id"
     Width="480px" AllowPaging="True"
     PagerSettings-Visible="false">
     <ItemTemplate>
       <asp:Label Text='<%# Eval("title") %>' runat="server" ID="descriptionLabel" />
       <Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("id") %>' />
     </ItemTemplate>
     ...

This sort of thing int id = (int)fvPhpto.FindControl("thumb1").PhotoID; does not work as PhotoID won't show up in intellisence.

This is what the code in Club:ImageThumbnail looks like:

public object PhotoID
{
    get
    {
        object o = ViewState["PhotoID"];
        return (o != null) ? (int)o : 0;
    }
    set
    {
        ViewState["PhotoID"] = (value != null && value !=DBNull.Value) ? Convert.ToInt32(value) : 0;     
    }
}

public ImageSizes ImageSize
{
    get
    {
        object o = ViewState["ImageSize"];
        return (o != null) ? (ImageSizes)o : ImageSizes.Thumb;
    }
    set
    {
        ViewState["ImageSize"] = value;
    }
}

public enum ImageSizes
{
    Large = 0,
    Thumb = 1,
    FullSize = 2
}

public string NoPhotoImg
{
    get
    {
        object o = ViewState["NoPhotoImg"];
        return (o != null) ? (string)o : null;
    }
    set
    {
        ViewState["NoPhotoImg"] = value;
    }
}

protected void Page_PreRender(object sender, System.EventArgs e)
{
    if (Convert.ToInt32(PhotoID) == 0)
    {
        if (NoPhotoImg != null)
        {
            Image1.ImageUrl = NoPhotoImg;
        }
        else
        {
            Image1.Visible = false;
        }
    }
    else
    {
        Image1.ImageUrl = "ImageFetch.ashx?Size=" + Convert.ToInt32(ImageSize) + "&ImageID=" + Convert.ToString(PhotoID);
    }
}


<asp:Image ID="Image1" runat="server" CssClass="photo" BorderWidth="1" />

1 Answer 1

1

You need to cast to the user control type first and then to int for the photo ID value, like this:

int id = (int)(fvPhpto.FindControl("thumb1") as ImageThumbnail).PhotoID;

It is probably easier and clearer to understand if you split apart the casts into two steps, first to the user control type and then cast to the int, like this:

ImageThumbnail theUserControl = fvPhpto.FindControl("thumb1") as ImageThumbnail;

// Since the C# as operator returns null for a failed cast, then we need to 
// check that we actually have an object before we try to use it
if(theUserControl != null)
{
    int id = (int)theUserControl.PhotoID;
}

Note: If ImageThumbnail is not the name of your user control's class, then change ImageThumbnail to the name of your user control's class name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.