check and add div inside div at run time
i have div in .aspx page:
<div id="mainWrapper" runat="server">
</div>
iwant to append div in r开发者_StackOverflow社区un time. In page load i try
mainWrapper.append("<div id="headerLeft"></div>");
but i am unbale to check if div id="headerLeft"
already exist or not.if not then only append div.Thanks.
use jquery
if ($("#headerLeft").length=0){
$("#mainWrapper").append('<div id="headerLeft"></div>');
}
you could use mainWrapper.FindControl
to check if the div already contains the other one and you add the inner one with mainWrapper.Controls.Add
only if not exists.
but anyway, if you do something like this:
protected Page_Load(...)
{
if(!IsPostBack)
{
// ... add the inner div...
}
}
you would add the inner div only once.
Wouldn't it be much simpler to just hide the headerLeft until you need it? ASP.NET will not output any HTML when the Visible
-Property is set to false:
<div id="mainWrapper" runat="server">
<div id="headerLeft" runat="server" Visible="false"></div>
</div>
Then in the code you can just:
headerLeft.Visible = true;
That way you can also easily check if the div is already visible.
精彩评论