Making a html element visible frm code behind

in my aspx page i have a div id=“testdiv” which whose visibility is hidden on load
from my code behind page i want to make the testdiv visible on fulfiling a certain condition
in codebehind page i am not getting testdiv
its saying its no delcared

how do i make it visible

If you add the runat=“server” attribute to any html element and give it an id you will get access to it from your code-behind.


<div id="testdiv" runat="server">...</div>


if (condition)
    testdiv.visible = true;

or:


if (condition)
    testdiv.Attributes.Add("style", "display:block");

<div id=“testdiv” runat=“server” style=“visibility:hidden; background-color:#FFFFFF; color:Green; width:264px; height:100px; top:250px;”>testing</div>
i have this in my aspx page

and this in code behind

testdiv.Visible = True

but not working

In that case you would wana use SlitheryImps eg.

Your way will only work if visible=“false” in the div itself, but you are using css to hide the div and thus have to use css to show it again.

Do you have to have it hidden by CSS for js? If not, the just remove the visibility:hidden from the style attr and do it like this:

<div id=“testdiv” runat=“server” visible=“false” style=“background-color:#FFFFFF; color:Green; width:264px; height:100px; top:250px;”>testing</div>

then you do not need to change ur codebehind

hi

its working . i removed the css and working fine
the only prblm now is i have a grid in this page
wht i want is when condition is true the results shld be dispalyed in grid and if condition is false div shld be displayed. its wrking fine
the only prblm is when the grid is displayed some space is left out . in that space i have put div

i dont want the grid to move down even if its not visible

you need to set the runat=“server” atttribute for that div and then through the code you can control its visibility:

For example
html code: <div id=“testdiv” run=“server”> … </div>
C# Code: testdiv.visible = false;

the div visiblity is done
now the prblm with div spacing?

What is the problem ? Can you please paste the code and explain. Thanks

Can you please paste the code, and explain the issue ?

Thanks.

Hmmm, im not exactally sure what you are trying to do.

But just something you should know that might help you figure it out. If you use visible=“false” on a runat server control, the control itself will not render at all. So the div will not be in the markup.

If you use the css method u originally did, it will be in the markup, but hidden and will still take up space.

<div id=“testdiv” runat=“server” visible=“false” style=“background-color:#FFFFFF; color:Green; width:264px; height:100px; top:250px;”>testing</div>

i have a grid and a div on the page
wht i want is when condition is true the results shld be dispalyed in grid and if condition is false div shld be displayed. its wrking fine

the only prblm is when the grid is displayed some space is left out at top . in that space i have put div. though the div is not visible when grid id visible the space is visible
i mean the grid is shifted down

i dont want the grid to move down even if its not visible

hmmm. That is hard to say. Sounds like a css or styling issue, but its hard to say, could you maybe post some code?