Set Container.DataItem property programmatically

Posted on 2006-04-04
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  = ???
Font-Bold = '<%#CBool(GetFontBold(Container.DataItem("XZY")).tostring) %>'            <--- set this kind of value
(do something with label)


Question by:codequest
    LVL 37

    Accepted Solution

    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 can create controls dynamically and add them to the " 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'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
    LVL 2

    Author Comment

    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.

    LVL 2

    Author Comment

    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.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now