Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:01:33+00:00 2026-05-24T09:01:33+00:00

I use C# asp.net4. I have a method to populate a Repeater with Anonymous

  • 0

I use C# asp.net4.

I have a method to populate a Repeater with Anonymous Types (Fields: Title, CategoryId), inside the Repeater I also placed a Label:

        var parentCategories = from c in context.CmsCategories
                               where c.CategoryNodeLevel == 1
                               select new { c.Title, c.CategoryId };
        uxRepeter.DataSource = parentCategories;
        uxRepeter.DataBind();

I need to change Text Properties for each label inside my Repeater on Repeater Event ItemDataBound

   protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        HyperLink link = (HyperLink)e.Item.FindControl("uxLabel");
        uxLabel.Text = // How to do here!!!!!!!! 
    }

So I need set the properties for Label.Text using e.Item (or a better way if any).

My problem I’m not able to CAST the e.Item (Anonymous type Field Title) and set it as Text Propriety for my Label.

I understand Anonymous Type can be casted to only Object Type, but in my case my Anonymous Type has Title and CategoryId Fields.

My question:

How to cast and retrieve the field with I’m interested? Thanks for your time on this?

EDIT:
SOME ERROR I RECEIVE:

Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.String'.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:01:34+00:00Added an answer on May 24, 2026 at 9:01 am

    The options Joseph presents are good ones, but there is a horrible way you can do this. It’s somewhat fragile, as it relies on you specifying the anonymous type in exactly the same way in two places. Here we go:

    public static T CastByExample<T>(object input, T example)
    {
        return (T) input;
    }
    

    Then:

    object item = ...; // However you get the value from the control
    
    // Specify the "example" using the same property names, types and order
    // as elsewhere.
    var cast = CastByExample(item, new { Title = default(string),
                                         CategoryId = default(int) } );
    var result = cast.Title;
    

    EDIT: Further wrinkle – the two anonymous type creation expressions have to be in the same assembly (project). Sorry for forgetting to mention that before now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application developed in ASP.net & C#. I also use Telerik
I use asp.net 4 c#. I have a gridview, inside a Label. I would
I use ASP.NET and have a label control on my page, which I fill
I use ASP.NET and have a Button and a CustomValidator, which has to validate
I have tried to use ASP.NET MVC for a while, then I face a
I use asp.net 4 c# and ef4. I have this code, it should compile
I use asp.net 4 c# and EF 4. I'm profiling my application. I have
I use ASP .Net 4 and I have a project using the form web
I have an asp page with Timer control. Timer control will trigger some method
I use asp.net 4 and c#. I have a TreeView and I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.