简体   繁体   中英

How to get the values from dynamically generated html textboxes in asp.net

I want to fetch the values from dynamically created html textboxes in my aspx page. Now first I want to check if the textbox exists or not. If exists then I want to fetch values from these texboxes. Here is the code which I am using for creating textboxes dynamically

<script type="text/javascript">
    var counter = 2;
    $(document).ready(function () {

        $("#addButton").click(function () {
            if (counter > 3) {
                alert("Limit Exceeds");
                return false;
            }
            var $wrap = $('#TextBoxesGroup');
            var dynamichtml = '<div id="div' + counter + '"><div class="mcity"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/>  </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 1 + '" class="auto"/> </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 11 + '" class="auto"/> </div>';
            $wrap.append(dynamichtml);
            counter++;
        });

        $("#removeButton").click(function () {
            if (counter == 1) {
                alert("No more textbox to remove");
                return false;
            }

            counter--;
            $("#TextBoxesGroup").find("#div"+ counter).remove();
          //  $("#TextBoxesGroup").find("#textbox" + counter+1).remove();


        });
  $(".auto").live("focus", function () {

                  $(this).autocomplete({

                      source: function (request, response) {
                          var textval = request.term; // $(this).val();
                          $.ajax({
                              type: "POST",
                              contentType: "application/json; charset=utf-8",
                              url: "Home.aspx/GetAutoCompleteData",
                              data: "{'code':'" + textval + "'}",
                              dataType: "json",
                              success: function (data) {
                                  response(data.d);
                              },
                              error: function (result) {
                                  alert("Error");
                              }
                          });
                      }
                  });
              });
          });

</script>

This code is generating textboxes generating and I am fetching data from my database using json autocomplete process.After this I am trying to send the information to another page using javascript the code that I write for this is below

<script type="text/javascript">
      $(function () {
          $("#btnPost").bind("click", function () {
              //Create a Form
              var $form = $("<form/>").attr("id", "data_form")
                            .attr("action", "Complete.aspx")
                            .attr("method", "post");
              $("body").append($form);

              //Append the values to be send
              AddParameter($form, "txtleft", $("#txtSearchleft").val());
              AddParameter($form, "txtright", $("#txtSearchright").val());

              //Send the Form
              $form[0].submit();
          });
      });
      function AddParameter(form, name, value) {
          var $input = $("<input />").attr("type", "hidden")
                                .attr("name", name)
                                .attr("value", value);
          form.append($input);
      }
    </script>

This work fine for textboxes which placed default in my page. But for the textboxes that generate dynamically after these textboxes I dnt know how to send values of those textboxes to my next page

Javascript and asp.net experts please help me to resolve this

Thanks

You can get the controls values using FormCollection . Note that it works on name rather than Id.

You need to keep the count and follow some naming convention for name of your controls.

Create a asp.net hidden field and keep the control count in that. And get according the values of the hidden field.
Msdn link

well if you were to assign your form an id you could just serialize your form and post it using ajax if you wanted

$.post("complete.aspx",$("#data_form").serialize(),function(response){
//react to the post here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM