ASP. Text box textbox (add textbox to page dynamically)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.