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

  • Home
  • SEARCH
  • 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 5945939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:45:07+00:00 2026-05-22T16:45:07+00:00

Edit: My object is defined as below. I passed it into GridView1. public class

  • 0

Edit: My object is defined as below. I passed it into GridView1.

public class UserTestorViewModel
{
    public string Username {get;set;}
    public string Password {get;set;}
    public Label SuiteName {get;set;}
}

I have GridView bound to a customized List<UserTestorViewModel>:

this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();

and in the .aspx I have

<asp:TemplateField HeaderText="LoginName">
<ItemTemplate>
<asp:Label ID="LoginName" runat= "server" Text= '<%# ((ViewModels.UserTestorViewModel)Container.DataItem).User.userName %> '></asp:Label>
</ItemTemplate>
</asp:TemplateField> 

this works because ViewModels.UserTestorViewModel.User.userName is string, but

<asp:TemplateField HeaderText="SuiteName">
<ItemTemplate>
<%# ((ViewModels.UserTestorViewModel)Container.DataItem).SuiteName %>
</ItemTemplate>
</asp:TemplateField>

because ViewModels.UserTestorViewModel.SuiteName is Label from System.Web.UI.WebControls

So how to bind a System.Web.UI.WebControls to <ItemTemplate> NOT the Text of the System.Web.UI.WebControls

  • 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-22T16:45:07+00:00Added an answer on May 22, 2026 at 4:45 pm

    You may place a Placeholder control in ItemTemplate and put your label control to it on the RowCreated event of the GridView:

        protected void Page_Init(object sender, EventArgs e)
        {
            GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
        }
    
        void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var dataItem = (KeyValuePair<int, Label>)e.Row.DataItem;
                var nameLabelPlaceholder = e.Row.FindControl("NameLabelPlaceholder") as PlaceHolder;
                if (nameLabelPlaceholder != null)
                {
                    nameLabelPlaceholder.Controls.Add(dataItem.Value);
                }
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = GetGridViewDataSource();
                GridView1.DataBind();
            }
        }
    
        private object GetGridViewDataSource()
        {
            return (from item in Enumerable.Range(1, 10)
                    select new KeyValuePair<int, Label>(item, new Label() { ID = string.Format("NameLabel_{0}", item), Text = string.Format("Item #{0}", item), ForeColor = System.Drawing.Color.Red }))
                        .ToDictionary(kvp1 => kvp1.Key, kvp2 => kvp2.Value);
        }
    
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Key">
            <Columns>
                <asp:BoundField HeaderText="Id" DataField="Key" />
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:PlaceHolder runat="server" ID="NameLabelPlaceholder" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface and a class defined as below public interface IShape {
--Edit with more bgnd information-- A (black box) COM object returns me a string.
-Edit- Alternative question/example How do i cast A to object to class A when
In the code below I can get the class name via: s.__class__.__name__ #Seq but
I have several objects in the database. Url to edit an object using the
Hi I'm having the following two actions defined in my controller [Authorize] [HttpGet] public
Let's say you have a class class C { int * i; public: C(int
Edit 5/11/2011: I guess it's a bit worse than what's below; in my deployed
I have an input form, nested within a div, defined like this: <div class=login-input>
I have a collection of stuffs in a generic Dictionary<string, object>() . The key

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.