Question

How to access a server-side div in code behind (inside content page)?
mean there is a div in content page like below:

<div id="MyDiv" runat="server">
</div>

but the code below does not work:

 MyDiv.Style.Add("display", "none"); 

EDIT
I am so sorry and that was my mistake!
The codes upper are correct and work perfect and there is no problem about them. My mistake was about my css and after fixing it every thing was ok.
so really thanks for attention and answers.

Was it helpful?

Solution

to access element from content page you can use below code:

HtmlGenericControl myDiv = (HtmlGenericControl)MyDiv;
myDiv.Style.Add("Display", "none");

OTHER TIPS

try that code:

MyDiv.Style["display"] = "none";

some other variants here:How do you change the style of a div programatically

What about:

MyDiv.Visible = false;

Note that this causes the div not to render at all on the page, so you can't access it on the client side to make it visible later.

note that if you set an id to your tag which includes non-allowed characters like (-) you will not be able to access it in code-behind.

<div id="myDiv" runat=server"> content goes here... </div>

in code-behind:

myDiv.visible=false;

that will work.

For those using Amir answer getting the 'The name '(element id)' does not exist in the current context' try opening the designer.cs file of your asp page and manually add your html element: protected global::System.Web.UI.HtmlControls.HtmlGenericControl elementidhere;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top