How to set css style value from code-behind?


F

Franklin

I have this line in an aspx page:

<div style="width:1000px;margin:0px auto ">

I want to set the width in the code-behind. I know I can add runat="server"
to the <div /> tag, add an ID value, and access the div from code behind,
like this:

<div runat="server" id="myDiv" style="width:1000px;margin:0px auto ">

But how to then set the style="width:1000px... value so that the width is
something other than 1000px?

Thanks!
 
Ad

Advertisements

E

Eliyahu Goldin

Use Style collection:
myDiv.Style["width"]="200px";

You can also replace the <div> with an <asp:panel>. It will produce the same
html output but will give you better choice of server-side properties.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Franklin said:
I have this line in an aspx page:

<div style="width:1000px;margin:0px auto ">

I want to set the width in the code-behind. I know I can add
runat="server" to the <div /> tag, add an ID value, and access the div
from code behind, like this:

<div runat="server" id="myDiv" style="width:1000px;margin:0px auto ">

But how to then set the style="width:1000px... value so that the width is
something other than 1000px?

Thanks!







__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4068 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4069 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
R

Ratnesh Maurya

Use Style collection:
myDiv.Style["width"]="200px";

You can also replace the <div> with an <asp:panel>. It will produce the same
html output but will give you better choice of server-side properties.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin




I have this line in an aspx page:
<div style="width:1000px;margin:0px auto ">
I want to set the width in the code-behind. I know I can add
runat="server" to the <div  /> tag, add an ID value, and access the div
from code behind, like this:
<div runat="server" id="myDiv" style="width:1000px;margin:0px auto ">
But how to then set the style="width:1000px... value so that the width is
something other than 1000px?

If you want to do it in Javascript

document.getElementById('id').style.width = '1000px';

-Cheers
Ratnesh
S7 Software
 
H

Hillbilly

I'll also recommend using the Panel control which renders as a div and
exposes its properties in code.

Eliyahu Goldin said:
Use Style collection:
myDiv.Style["width"]="200px";

You can also replace the <div> with an <asp:panel>. It will produce the
same html output but will give you better choice of server-side
properties.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Franklin said:
I have this line in an aspx page:

<div style="width:1000px;margin:0px auto ">

I want to set the width in the code-behind. I know I can add
runat="server" to the <div /> tag, add an ID value, and access the div
from code behind, like this:

<div runat="server" id="myDiv" style="width:1000px;margin:0px auto ">

But how to then set the style="width:1000px... value so that the width is
something other than 1000px?

Thanks!







__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4068 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4069 (20090512) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Ad

Advertisements

A

Arijit

Hi Franklin

there are several ways, i am describing two

1) In aspx write like this
<div id="div1" runat="server" >
...........
...........
</div>

In Page_Load of code behind
div1.Attributes.Add("style", "width:1000px;margin:0px auto");

2) Add a Literal control in aspx page
In Page_Load of code behind
Literal1.Text = "<div style=\"width:1000px;margin:0px auto\"></div>";

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top