Click here to Skip to main content
15,896,268 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to generate div inside content place holder using javascript.... please help me..


XML
<asp:Content ID="Content1" ContentPlaceHolderID="cphmain" Runat="Server">
<script type="text/javascript" language="javascript">
    onload = function () {
        for (var i = 0; i < 3; i++) {

            var ph = document.getElementById('<%=Content1.ClientId%>');
            alert(ph);
            var div = document.createElement("div");
            div.setAttribute("id", "rounddivepanel");

            document.body.appendChild(div);

        }
    }
</script>
<div id="contents">
<div id="maindiv" style="padding-left:150px">


</div>
</div>
</asp:Content>
Posted
Comments
[no name] 11-Jan-13 8:27am    
What is the issue in this code?
rahemani_1200 11-Jan-13 8:40am    
i want to display div "rounddivepanel" inside of content place holder
Sandeep Mewara 11-Jan-13 9:46am    
What have you tried? Any error/issue?
[no name] 11-Jan-13 9:53am    
This is one line of code with jQuery, is jQuery an option?

As far as I'm aware, content place holder tags don't even get passed to the client, they're for server-side use only, so you won't be able to access them with Javascript like this.

An easy way to do this is to put a <div> tag around the place holder in your Master page, then use that tag to add your content to instead.

So in your Master page:

ASP.NET
<div id="mainContent"> <!-- add this line here -->

        <!-- main content placeholder -->
        <asp:contentplaceholder id="cphmain" runat="server" >

        </asp:contentplaceholder>


</div>


Then in the Javascript on your content page:
JavaScript
var ph = document.getElementById('mainContent');

var divtest = document.createElement('div');

divtest.setAttribute('id', 'rounddivepanel');

divtest.innerHTML = 'my new div is here';

ph.appendChild(divtest);
 
Share this answer
 
v5
I sugguest you use jQuery, neat and easy!

http://api.jquery.com/hide/[^]
 
Share this answer
 

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