从客户端获取文本框值 [英] Get textbox value from clientside
本文介绍了从客户端获取文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已在客户端编写了添加文本框的代码,如下所示:
我的问题是我不知道如何从文本框中获取价值?我想将值发送到我的数据库。
I have written code for add textbox in clientside as below:
my problem is I dont know how to get value from text boxes? I want to send values to my DB.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<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="form1" 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" />
<asp:Label ID="lbltest" Text="" runat="server"></asp:Label>
</form>
</body>
</html>
protected void Post(object sender, EventArgs e)
{
string[] textboxValues = Request.Form.GetValues("DynamicTextBox");
JavaScriptSerializer serializer = new JavaScriptSerializer();
this.Values = serializer.Serialize(textboxValues);
string test;
test=this.Values;
lbltest.Text = test;
}
推荐答案
TextBox呈现为HTML元素输入
,所以它的文本可以通过值
属性访问: http:// www .w3schools.com / tags / att_input_value.asp [ ^ ]。
-SA
TextBox is rendered as HTML elementinput
, so it's text is accessible via thevalue
attribute: http://www.w3schools.com/tags/att_input_value.asp[^].
—SA
这篇关于从客户端获取文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文