0

I want to create a auto populate textbox in asp.net. However I am unable to populate the textbox dynamically. Any help would be very appreciated.

Javascript code

$( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "Haskell",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });

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

       $( "#tags" ).autocomplete({
        disabled: true
      });

});
    $("#enable").click ( function() {

       $( "#tags" ).autocomplete({
        disabled: false
      });

});

  } );

I have tried to populate from asp.net but I am unable to make it work here is what I have tried.

            string[] availableTags = new string[]{
            "Las Vegas",
            "Los Angeles",
            "Tampa",
            "New York",
            "s",
            "sss"
            };

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                JavaScriptSerializer serializer = new 

                JavaScriptSerializer();
                    string jsArray = serializer.Serialize(availableTags);
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "availableTags", jsArray, true);
    }
 }

1 Answer 1

1

You can add script tag and use variable with <%= %>

<script type="text/javascript">
     var myArray = '<%= jsArray %>';
     console.log(JSON.parse(myArray);
 </script>

In your .cs source code note that public before jsArray

         string[] availableTags = new string[]{
            "Las Vegas",
            "Los Angeles",
            "Tampa",
            "New York",
            "s",
            "sss"
            };
           public string jsArray = string.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                JavaScriptSerializer serializer = new 

                JavaScriptSerializer();
                jsArray = serializer.Serialize(availableTags);
             }

    }

Update script

$( function() {
     var myArray = '<%= jsArray %>';
    var availableTags = JSON.parse(myArray);
    $( "#tags" ).autocomplete({
      source: availableTags
    });

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

       $( "#tags" ).autocomplete({
        disabled: true
      });

});
    $("#enable").click ( function() {

       $( "#tags" ).autocomplete({
        disabled: false
      });

});

  } );
Sign up to request clarification or add additional context in comments.

3 Comments

I get the following error the name 'jsArray' does not exist in the current context?
Thank you that fix that problem however nothing happens when I start typing. var availableTags = '<%= jsArray %>'; $(function () { console.log(JSON.parse(availableTags)); $( "#tags" ).autocomplete({ source: availableTags }); $("#disable").click ( function() { $( "#tags" ).autocomplete({ disabled: true }); }); $("#enable").click ( function() { $( "#tags" ).autocomplete({ disabled: false }); });
I updated answer for script, you need use JSON.parse to array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.