Set Container.DataItem property programmatically

Posted on 2006-04-04
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)


Question by:codequest
  • 2
LVL 37

Accepted Solution

samtran0331 earned 2000 total points
ID: 16377636
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

Author Comment

ID: 16377809
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.


Author Comment

ID: 16385745
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

864 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