Retriving textbox values

Retriving textbox values

So I just picked up JQuery yesterday and I am trying to access the text of a dynamically created text box.

Here is the code I am using.  Please let me know what I am doing wrong.


  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

    <!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>Untitled Page</title>
        <script src="Scripts/jquery-1.4.2.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
            var counter = 0;
           
                $("#btnOfficial").click(function() {
                    var hdValue = $("#theValue");
                    var num = ($("#theValue").val() - 1) + 2;
                    hdValue.val(num);
                    $("#Div1").append("<input type=\"text\" id=textbox" + (counter) + "\" />");
                    $("#Div1").append("<br />");
                    counter++;
                });
               
                $("#btnSave").click(function() {
                alert("saveClicked");
                    var hdValue = $("#theValue").val();
                    var msg = "";
                    for(i=0; i<counter; i++){
                        msg += "\n Textbox #" + i + " : " + $("#textbox" + i).val();
                    }
                    alert(msg);
                    $("#tbxValues").val(msg);
                    counter = 0;
                });
            });
        </script>

    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager runat="server" ID="sm1" />
            <asp:UpdatePanel runat="server" ID="myUpdatePanel1">
                <ContentTemplate>
                    <div id="Div1" runat="server">
                    </div>
                    <asp:GridView ID="GridView1" runat="server">
                    </asp:GridView>
                    <input type="button" id="btnOfficial" value="Add Another TextBox" />
                    <input type="hidden" value="0" id="theValue" runat="server" />
                    <input type="hidden" id="tbxValues" runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:Button ID="btnSave" runat="server" Text="Read" OnClick="btnSave_Click" />
        </form>
    </body>
    </html>




















































The problem is in the btnSave.click function the message always says that the .val() is undefined even when there is text in the textbox.


Thanks in advance