Set Container.DataItem property programmatically

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
                                                                                                                                           programmatically
(do something with label)
=========================

Thanks!


LVL 2
codequestAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

samtran0331Commented:
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"
            Else
                .CssClass = "MyLabelCSS2"
            End If
            .Text = "Time is: " & Now().ToString
        End With
        PlaceHolder1.Controls.Add(lb)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codequestAuthor Commented:
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,  

and

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.

Grazie!
codequestAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.