Modifying DIV tag style at code behind (VB).NET


I am unable to access the div tag in code behind so that I can assign a CSS sytle to it. I need to set the CSS style of a div tag in the code behind:

<div id="divFooter" runat="server"><asp:Textbox... /></div>

I tried:

Dim divFooter As New HtmlGenericControl
        divFooter = Me.FindControl("divFooter")
        divFooter.Attributes.Add("class", "textboxes_gray")


divFooter.CSSclass = "textboxes_gray"

but neither work.

Many thanks.

Who is Participating?

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

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.

Göran AnderssonCommented:
The HtmlGenericControl doesn't have a CssClass property. You have to use the Attributes collection:

divFooter.Attributes("class") = "textboxes_gray"

Open in new window

GreenGhost is correct, you have to use the Attributes collection.

The other option is to use a Panel instead of the div.  The Panel is rendered as a div on the page.
hermes_consultingAuthor Commented:
The problem is actually accessing the div tag itself. Calling it as divFooter in code behind does not work - run time error comes up divFooter does not exist. (That is why I had used HTMLGenericControl in front of it).

Please help, I looked all over the web.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

There doesn't appear to be anything wrong with the div tag that you have shown.

The most likely scenario is that it's contained within something like a repeater.

Can you post the contents of your .aspx page so that we can see where the div is with relation to other tags on the page.
your Div is not created as control
Check your Designer file "pagename.designer.vb"

It should have code mentioned below

if it does not exist then you need to create it.
better now place that in your code behind file
	'''divFooter control.
	'''Auto-generated field.
	'''To modify move field declaration from designer file to code-behind file.
	Protected WithEvents divFooter As Global.System.Web.UI.HtmlControls.HtmlGenericControl

Open in new window


You have to add the Attribute from server side for div tag.

Use following code to add the CSS style to your DIV tag.

divFooter.Attributes.Add("class", "cssclassnamefordiv");
Göran AnderssonCommented:
niralshah, I covered that a month ago...
hermes_consultingAuthor Commented:
The divtag is not in any container, not using a repeater.

Do I need to add runat="server" to the div tag so that is shows up in code behind?
Yes you need to add runat="server" to div tag to show up in code behind.

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
hermes_consultingAuthor Commented:
Many thanks, suspected as much finally...
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
Web Development

From novice to tech pro — start learning today.