ساخت دايناميكي تگ HTML

ساخت دايناميكي تگ <DIV> با استفاده از جاوااسكريپت

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Create DIV Dynamically Using JavaScript</title>
<script type="text/javascript" language="javascript">
   1:     
   2:   function DynamicDiv() {        
   3:         var dynDiv = document.createElement("div");
   4:         dynDiv.id = "divDyna";
   5:         dynDiv.innerHTML = "Created using JavaScript";
   6:         dynDiv.style.height = "20px";
   7:         dynDiv.style.width = "300px";      
   8:         dynDiv.style.backgroundColor = 'gray';
   9:         document.body.appendChild(dynDiv);
  10:     }
</script> </head> <body> <div> <input id="Button1" type="button" value="Using JS" onclick="DynamicDiv();" /> </div> </body> </html>



همين كار با استفاده از ASP.NET




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs"
Inherits="Default5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title>Create DIV Dynamically Using ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnCode" runat="server"
onclick="btnCode_Click"
Text="Code Behind" /> 
</div>
</form>
</body>
</html>



كد با #C




protected void btnCode_Click(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "dynDivCode";
dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
dynDiv.InnerHtml = "I was created using Code Behind";
this.Controls.Add(dynDiv);
}



كد با VB.NET




Protected Sub btnCode_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dynDiv As New System.Web.UI.HtmlControls.HtmlGenericControl("DIV")
dynDiv.ID = "dynDivCode"
dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray")
dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px")
dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px")
dynDiv.InnerHtml = "I was created using Code Behind"
Me.Controls.Add(dynDiv)
End Sub



منبع

نظرات

پست‌های معروف از این وبلاگ

مقدمه ای بر RavenDB – قسمت دوم

مقدمه ای بر RavenDB – قسمت سوم

lnav ابزاری بسیار کاربردی برای پیمایش لاگ ها در لینوکس و البته مک