We help IT Professionals succeed at work.

Set Container.DataItem property programmatically

codequest asked
Medium Priority
Last Modified: 2008-02-01
is it possible to set values like the one below for a label, programmatically?   If so, in what event would that take place, and what would the syntax look like?   Or, if there are reasons why this can only done on the page itself, that would be helpful to know.
dim label1 as new label
label.style.item...???  = ???
Font-Bold = '<%#CBool(GetFontBold(Container.DataItem("XZY")).tostring) %>'            <--- set this kind of value
(do something with label)


Watch Question

Top Expert 2007
here's the thing...and others can argue the point if they like...but imho....using the "style" tag and defining properties like the "Font-Bold" for a control is not the best way to design.
I never touch any of the visual settings of a control (like forecolor, backcolor, borderstyle, etc.) in Visual Studio.
The "style" part of a tag is a horrible way to control visual style....and anything you come up with to manage a style tag programmatically is making work for yourself you don't need...

Imho, you should be handling all of it with CSS or if youre in 2.0, you should be using themes and CSS...with that said...you can create controls dynamically and add them to the page...google "asp.net dynamic controls dynamically adding controls" for tutorials...but the syntax would be something like below....the If statement that sets the CssClass is one way to do what you want to do...you'd also need to either have a separate style sheet or have the styles in the <head>...tons of CSS tutorials out there....
also added a few properties like backcolor to show you that some of those visual elements can be worked with...but i'd suggest reading up on CSS and theming and skinning if you're using 2.0

        Dim lb As System.Web.UI.WebControls.Label = New System.Web.UI.WebControls.Label
        With lb
            .BackColor = Drawing.Color.AliceBlue
            .BorderColor = Drawing.Color.Aquamarine
            .BorderStyle = Web.UI.WebControls.BorderStyle.Solid
            .ForeColor = Drawing.Color.Black
            If 1 = 1 Then
                .CssClass = "MyLabelCSS"
                .CssClass = "MyLabelCSS2"
            End If
            .Text = "Time is: " & Now().ToString
        End With

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks for the input.   I'd like to go in that direction and....

I need to make sure I can dynamically add controls to a panel in a gridview item template, the "how to" for which I'm having to find in the haystack,  


I'm going to be changing the visibility of labels, buttons and textboxes, and also their left position and width within the panel, based on Container.Item values and other conditions.

It seems like to do that I have to generate the css in advance, one for every possible case.  Which isn't impossible, because there are going to be a finite number of cases, but it makes the programming so difficult to control.   The key variables that I'm working with are spread out between the code behind and the style sheet, and there's the class id as an extra text name to remember.   If I want to make a change or adjustment relative to more than one control, you're having to keep a lot of stuff in my head at one time.  Since I have problems in that area, it seems more challenging.

I guess I don't understand what is fundamentally wrong with using the style variable.   I do know I've had much better luck controlling tables when I use styles, but that's not the case here.    Do you have any more behind the dislike of using styles.

Regardless, I'll give serious consideration to you input, because I've heard similar on several occasions.



Doing the research I recognize that the question was incoherent...if you're programmatically adding controls, then you set their values right then, or call the procedure directly,

Font-Bold = GetFontBold(somevariable)    

not through

Font-Bold = '<%#CBool(GetFontBold(Container.DataItem("XZY")).tostring) %>'    

.....pretty bizarre in retrospect!

Anyway, thanks for the prompting on this one.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.