Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
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.

ASP.NET
<%@ 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>



C#
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;
   }
Posted
Updated 1-Sep-13 3:25am
v8
Comments
Dave963 1-Sep-13 8:59am    
Code snippet rendered really funny, could you maybe just try to repost the html code

1 solution

TextBox is rendered as HTML element input, so it's text is accessible via the value attribute: http://www.w3schools.com/tags/att_input_value.asp[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900