how to append <div> dynamically to created <div>using server side logic not by using jQuery

Ashok Kumar 221 Reputation points
2022-12-04T15:52:33.267+00:00

I know by using jQuery logic we can append n numbers of <div>'s dynamically to already created <div> but my question is, Can we write server side logic to create a dynamic <div>'s when the page is loaded (same like jQuery logic).

This is jQuery logic

   $(document).ready(function(){  
     
   var i=0,length=2;  
     
        for(i; i<=length;i++)  
    {  
     $('#footer-div').append($('<div class="dynamic"><h3 class="ui-title" role="heading" aria-level="1">Dynamic div creating</h3></div>'));   
    }  
     
   )};  

Please suggest me is it possible to create dynamic <div>'s by using server side logic using asp.net c#?

If it is possible then what is the main difference between jQuery logic and server side logic?

And which is the best approach to achieve this ?

Please suggest me with example code.

Thanks

Microsoft 365 and Office | Development | Office JavaScript API
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-12-05T06:30:31.237+00:00

    Hi @Ashok Kumar ,
    You can directly use the StringBuilder.Append method to achieve similar functionality. You need to add the attribute runat="server" to your div tag.
    https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.append?view=net-7.0
    <div id="DIV1" runat="server"></div>


     StringBuilder sbHtml = new StringBuilder();  
     sbHtml.Append("<div class='dynamic'><h3 class='ui-title' role='heading' aria-level='1'>Dynamic div creating</h3></div>");         
     DIV1.InnerHtml = sbHtml.ToString();  
    

    Difference between Server Side Scripting and Client Side Scripting
    The browser executes jquery. Use it when the browser has all the code. The source code is intended to be transmitted over the Internet from the web server to the user's computer and run directly on the browser. It is also used for validation and functionality of user events.
    Web servers are used to execute server-side scripts. They are basically used to create dynamic pages. It can also access file systems residing on network servers.
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-04T17:15:52.047+00:00

    Server side runs when the html is built on the server, jquery run in the browser after the browser loads the page.

    To do the code serverside, it depends on the framework used. In webform it would be a repeater with templates, in mvc views it’s similar

    <div I’d=“footer-div”>  
    @for(var i = 0; i<2; ;i++)  
     {  
        <div class="dynamic">  
             <h3 class="ui-title" role="heading" aria-level="1">  
                    Dynamic div creating  
             </h3>  
         </div>  
     }  
    </div>  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.