2

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.

4

5 回答 5

5

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

HtmlGenericControl myDiv = (HtmlGenericControl)MyDiv;
myDiv.Style.Add("Display", "none");
于 2012-11-25T16:34:26.473 回答
0

try that code:

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

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

于 2012-11-25T16:28:42.710 回答
0

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.

于 2012-11-25T18:42:53.497 回答
0

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.

于 2013-06-22T14:04:43.447 回答
0

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;

于 2018-07-20T20:12:08.253 回答