views:

424

answers:

1

i am creating the textboxes dynamically using jquery on a button click.

<table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <div id="lblCustName">
                </div>
            </td>
            <td>
                <div id="lblRemove">
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" value="Add" id="AddRow" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Submit" id="SaveCustomers" />
            </td>
        </tr>
        </table>

     <script type="text/javascript" language="javascript">
        $(document).ready(function() {
           //validate post form
            $("#CustomersForm").validate({ ignore: ":not(:visible)" });
           //Initialize the dynamic id
           _DynId = 1;
           //Click Event for Customers
            $('#AddRow').click(function(e) {
                $('#lblCustName').append('<div id="lblCustName' + _DynId + '" style="height:20px;padding-left:10px;">' + '<input type="text" name="CustName" id="CustName' + _DynId + '" class="required" value="" title="*" />');
                $('#lblRemove').append('<div id="lblRemove' + _DynId + '" style="height:20px;padding-left:10px;">' + '<img src="../../Content/Images/delete_icon.gif" onclick=Remove("' + _DynId + '"); title="Remove" id="iRemove' + _DynId + '"></img>');
                _DynId += 1;
            });
 </script>

I am trying to add the textbox for customers dynamically. This is the sample html for the functionality of adding customers for a report processing,... I need to give the autocomplete option for the textboxes.

I have used the below script for that:

$.getJSON("/User/GetAllCustomers/?t=" + new Date(), {},
      function(data) {
          if (data != null) {
              $("input:text[name=CustName]").autocomplete(data, { mustMatch: false, matchContains: 4, max: 50,
                  formatItem: function(row) {
                      if (row.CustomerName!= "") {
                          return row.CustomerName;
                      }
                  },
                  formatResult: function(row) {
                      return row.CustomerName;
                  }
              }).result(function(e, row) {
                 //do something
              });
          }
      });

The autocomplete is not working for the dynamically added controls.

I am trying for any possibility to register the dynamically added controls to the form, so that the autocomplete will work for the dynamically added controls

+1  A: 

The problem is that when you bind that autocomplete it's binding to the fields that match the selector when the function is ran. If you add something later that also matches it won't automatically bind.

For basic jQuery events there's a Live binding that lets you do this automatically, but autocomplete won't work through this - basically your only choice is to bind a new auto-complete each time you add a new text field

EG: Add to the AddRow function:

$("#lblCustName" + _DynId + " input[name=CustName]").autocomplete(...);

(Obviously replacing ... with the parameters you want)

You'll likely want to put your data object that you pulled back from the webservice in a global variable to support this, and probably the same with the autocomplete options (just to avoid code duplication).

Tim Schneider
if i have thousands of customers, then as per your answer if i bind each time i add a new control, then it has to call the database unnecessarily on new control add, which will cause performance issue on application side. Any ideas to avoid the database calls. I can't use caching too, as the users may have different number of customers .
Prasad
If you store the result of your getJSON call in a global JavaScript variable (Or namespaced if you want to be tidy) then you won't have to call back to the server for each one added, and could instead re-use the existing variable each time. That's what I was referring to in the last paragraph of my post. You can still use output caching by the way, you should look into the VaryBy argument (If you vary the output cache by user ID then each user's info will be cached separately), but it's still always faster if JavaScript doesn't need to make a round trip even if it's not going to hit the db.
Tim Schneider