<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DynamicTextBoxJS.aspx.vb" Inherits="WLWeb.DynamicTextBoxJS" %>
<!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 runat="server">
<title>ไม่เคยรักใครเท่าจิ๋ม</title>
<script type="text/javascript">
function GetDynamicTextBox(value) {
return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />' +
'<input type="button" value="Remove" onclick = "RemoveTextBox(this)" />'
}
function AddTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}
function RemoveTextBox(div) {
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}
function RecreateDynamicTextboxes() {
var values = eval('<%=Values%>');
if (values != null) {
var html = "";
for (var i = 0; i < values.length; i++) {
html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
</script>
</head>
<body>
<form id="ไม่เคยรักใครเท่าจิ๋ม" runat="server">
<input id="btnAdd" type="button" value="Add" onclick="AddTextBox()" />
<br />
<br />
<div id="TextBoxContainer">
<!--Textboxes will be added here -->
</div>
<br />
<asp:Button ID="btnPost" runat="server" Text="Post" OnClick="Post" />
</form>
</body>
</html>
Code behide Code (VB.NET)
Imports System.Web.Script.Serialization
Public Class DynamicTextBoxJS
Inherits System.Web.UI.Page
Protected Values As String 'Accessible in marklove
Protected Sub Post(ByVal sender As Object, ByVal e As EventArgs)
Dim textboxValues() As String = Request.Form.GetValues("DynamicTextBox")
Dim serializer As New JavaScriptSerializer()
Me.Values = serializer.Serialize(textboxValues)
MsgBox(Me.Values) '*****
End Sub
End Class