Click here to Skip to main content
15,663,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to generate dynamic html textboxes in my aspx page and on these textboxes i want to add the value using autocomplete facility. I try my best to do so. I try almost every single question's answer of stackoverflow. But nothing is working here my script which generate new textbox dynmacally

JavaScript
<script type="text/javascript">
    $(document).ready(function () {

        var counter = 2;

        $("#addButton").click(function () {

            if (counter > 5) {
                alert("Limit Exceeds");
                return false;
            }

            var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);

//            newTextBoxDiv.after().html('<label>Textbox #' + counter + ' : </label>' +
//        '<input type="text" name="textbox' + counter +
//        '" id="textbox' + counter + '" value=""  class="auto">');

            newTextBoxDiv.after().html('<div class="fields-left"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/>  </div><div class="fields-right"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter+1 + '" class="auto"/> </div>');
           newTextBoxDiv.appendTo("#TextBoxesGroup");


            counter++;
        });

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

            counter--;

            $("#TextBoxDiv" + counter).remove();

        });


    });


</script>


Html code

HTML
<div id="TextBoxDiv1" class="fields" >
          <div id="TextBoxesGroup">
          </div>
     </div>
          <input type='button' value='Add Button' id='addButton'/>
 <input type='button' value='Remove Button' id='removeButton'/>
           </div>


and I am trying this json code for fetching data for very first textbox that generate automatically. First I think for write this script for everytextbox which generate dynamically but this process will be so lengthy and wrong way to do this thing. But this is not working for me

JavaScript
<script type="text/javascript">
              $(document).ready(function () {
                  SearchText2();
              });
              function SearchText2() {
                  $("#textbox2").autocomplete({
                      source: function (request, response) {
                          $.ajax({
                              type: "POST",
                              contentType: "application/json; charset=utf-8",
                              url: "Home.aspx/GetAutoCompleteData",
                              data: "{'code':'" + document.getElementById('textbox2').value + "'}",
                              dataType: "json",
                              success: function (data) {
                                  response(data.d);
                              },
                              error: function (result) {
                                  alert("Error");
                              }
                          });
                      }
                  });
              }
</script>


Please experts tell me why this json is not working for me

Thanks
Posted
Updated 9-May-14 2:33am
v2

1 solution

 
Share this answer
 
Comments
AZAD CHOUHAN 9-May-14 8:46am    
but it is for mvc not for simple asp.net
Sanket Saxena 9-May-14 8:50am    
Dear the data return is json you can return the json data from your method "GetAutoCompleteData" simple as that. Its a jQuery dosen't matter Asp.Net or Asp.Net MVC it will work.
AZAD CHOUHAN 9-May-14 9:04am    
<script type="text/javascript">
$(document).ready(function () {
setupAutoComplete();
var counter = 2;

$("#addButton").click(function () {

if (counter > 5) {
alert("Limit Exceeds");
return false;
}

var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);

// newTextBoxDiv.after().html('<label>Textbox #' + counter + ' : </label>' +
// '<input type="text" name="textbox' + counter +
// '" id="textbox' + counter + '" value="" class="auto">');

newTextBoxDiv.after().html('<div class="fields-left"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/> </div><div class="fields-right"> <label> Going to</label> <input type="text" name="textbox' + counter + '" id="textbox' + counter + 1 + '" class="auto"/> </div>');
newTextBoxDiv.appendTo("#TextBoxesGroup");

setupAutoComplete();
counter++;
});

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

counter--;

$("#TextBoxDiv" + counter).remove();

});


});


var setupAutoComplete= function SearchText2() {
$('.auto').autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Home.aspx/GetAutoCompleteData",
data: "{'code':'" + document.getElementById("#textbox2").value + "'}",
dataType: "json",
success: function (data) {
response(data.d);

},
error: function (result) {
alert("Error");
}
});
}
});
}

</script>

I update my code to this now i am getting this error
TypeError: document.getElementById(...) is null

I dnt know why its show me empty ?
Sanket Saxena 9-May-14 9:10am    
well tell me about your previous code. Is that was calling the GetAutoCompleteData method in the code behind? if yes then what this function returns?

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