Click here to Skip to main content
15,887,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there any way I can remove a div (html element) from the code behind in ASP.Net.

ie: if (blah == true) { div1.remove } else { div2.remove }
Posted

There is nothing to remove, as div (or any DOM element) does not exist on the server side, which just generates HTML code in HTTP response. So, DOM object does not exist at that time, it is created only on client side after it receives the HTTP response and processes it. "Removal" would simply mean not generating those elements in next postback, which you can do in the class System.Web.HttpResponse, http://msdn.microsoft.com/en-us/library/system.web.httpresponse.aspx[^].

What you can do it to program deletion of the element in JavaScript, depending on some condition. If this is not clear so far, you can try to describe the behavior you want to achieve. In the meanwhile, it would be good for your to review the ASP.NET page life cycle:
http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx[^].

—SA
 
Share this answer
 
hi,
try like this will solve your problem
C#
ClientScript.RegisterStartupScript(this.GetType(), "remove", "<script>$('#div1').remove();</script>");
 
Share this answer
 
you can acces html element from server side code if you add runat="server" property to it....

then also remove an element is quite difficult.. but you can try some indirect ways...
eg...

XML
<div id="divparent" runat="server">
        <div id="divchild">
        //your div content here
        </div>
        </div>


now to remove divchild you can try this from c#...

if(your condition is true)
{
C#
divparent.InnerHtml = "";

}

you can try
C#
divparent.InnerText = &quot;&quot;;
or
divparent.Visible = false;


you can do this without setting runat="server" using javascript...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900