The following function generates a TextBox each time it executes (actually <input type= "Text" >)
var i=0;
function Changeit ()
{
if (i<5)
{
i=i+1;
skils= ' skill ' +i;
my_div.innerhtml = my_div.innerhtml + "<input type= ' text ' id= '" + skils + ">";
}
Else
{
}
}
Let's do it: Add a TB by clicking on the following:
<a id= "Addskills" href= "Javascript:changeit ();" >add more</a>
OK, the text box is displayed, but it is generated on the client, can not save the state, and the server can not communicate directly, their values how can we fetch it? --hidden Field, old-fashioned ...
<asp:hiddenfield id= "Skills" runat= "Server"/>
function GetValues ()
{
document.getElementById ("Skills"). Value=document.getelementbyid (' Skill1 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill2 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill3 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill4 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill5 '). value+ "|";
Window.alert (document.getElementById ("Skills"). Value);
}
In this way, the values of these textbox are deposited in the hidden Field, and we take a look at the service side:
<asp:button id= "Button3" onclientclick= "javascript:getvalues ();" runat= "Server"
Text= "Get from Client-side" onclick= "Button3_Click"/>
protected void Button3_Click (object sender, EventArgs e)
{
String Val=this. Skills.value;
Response.Write (Val);
}
Take It out:
All code here:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>untitled page</title>
<script language= "javascript" type= "Text/javascript" >
var i=0;
function Changeit ()
{
if (i<5)
{
i=i+1;
skils= ' skill ' +i;
my_div.innerhtml = my_div.innerhtml + "<input type= ' text ' id= '" + skils + ">";
}
Else
{
}
}
function GetValues ()
{
document.getElementById ("Skills"). Value=document.getelementbyid (' Skill1 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill2 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill3 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill4 '). value+ "|";
document.getElementById ("Skills"). Value+=document.getelementbyid (' Skill5 '). value+ "|";
Window.alert (document.getElementById ("Skills"). Value);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:hiddenfield id= "Skills" runat= "Server"/>
<a id= "Addskills" href= "Javascript:changeit ();" >add more</a>
<asp:button id= "Button3" onclientclick= "javascript:getvalues ();" runat= "Server"
Text= "Get from Client-side" onclick= "Button3_Click"
/>
<div id= "My_div" >
</div>
</div>
</form>
</body>
Using System;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button3_Click (object sender, EventArgs e)
{
String Val=this. Skills.value;
Response.Write (Val);
}
}
ASP. Text box textbox (add textbox to page dynamically)