Solved

jquery, aspx

Posted on 2016-09-07
10
64 Views
Last Modified: 2016-09-07
When both below html controls are on change,
I want to fire up GetData and fill up the product_subcode dropdown.
Can you show me how to do it in jquery?
 <select   runat="server" required class="form-control" id="PRODUCT_STATE" name="PRODUCT_STATE">
  <option value="" disabled="disabled" selected="selected" class="disabled">Product State</option>  
  <option value="ND">ND</option>        
  <option value="OH">OH</option>                                                                                                                
  <option value="ZZ">All Other States</option>                                            
</select>        
<select runat="server" required class="form-control" id="PRODUCT_SUBCODE" name="PRODUCT_SUBCODE">
  <option value="" disabled="disabled" selected="selected" class="disabled">Product Code 2</option>
</select>

Open in new window

Default.aspx

[WebMethod()]
public static List<string> GetData(string state)
{
  List<string> tmpStr = new List<string>();
  if (state == "ND")
  {
    tmpStr.Add("Ricky");
    tmpStr.Add("John");
    tmpStr.Add("Tom");
  }
  else
  {
    tmpStr.Add("Mary");
    tmpStr.Add("Susan");
  }
  return tmpStr;
}

Open in new window

0
Comment
Question by:ITsolutionWizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41787654
1. Make sure your class is decorated with the ScriptService decorator
[ScriptService]

2. Remove 'static' from GetData WebMethod

3. You can then use this jQuery
<script>
$(function () {
    $('#PRODUCT_STATE').change(function () {
        $.ajax({
            url: 'Default.aspx/GetData',
            data: '{"state":"' + $(this).val() + '"}',
            contentType: "application/json; charset=utf-8",
            type: 'POST',
            dataType: 'JSON'
        }).done(function (resp) {
            var sc = $('#PRODUCT_SUBCODE').empty();
            $.each(resp.d, function (i, d) {
                sc.append($('<option/>').val(d).html(d));
            });
        });
    });
});
</script>

Open in new window

0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 41788081
". Make sure your class is decorated with the ScriptService decorator" - What does it mean?
Can you show me in codes?
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 41788092
the codes do not seem working
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 41788111
I make quick testing page below. and using same webmethod. And does not work.
Please correct me if I done anything wrong...

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestMe.aspx.cs" Inherits="AppService.TestMe" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
    $(function () {
        $('#PRODUCT_STATE').change(function () {
            $.ajax({
                url: 'TestMe.aspx/GetData',
                data: '{"state":"' + $(this).val() + '"}',
                contentType: "application/json; charset=utf-8",
                type: 'POST',
                dataType: 'JSON'
            }).done(function (resp) {
                var sc = $('#PRODUCT_SUBCODE').empty();
                $.each(resp.d, function (i, d) {
                    sc.append($('<option/>').val(d).html(d));
                });
            });
        });
    });
</script> 
        
         <select   runat="server" class="form-control" id="PRODUCT_STATE" name="PRODUCT_STATE">
                                                                    <option value="" disabled="disabled" selected="selected" class="disabled">Product State</option>   
                                                                    <option value="ND">ND</option>         
                                                                    <option value="OH">OH</option>                                                                                                                 
                                                                    <option value="ZZ">All Other States</option>                                             
                                                                </select>         
 <select runat="server" class="form-control" id="PRODUCT_SUBCODE1" name="PRODUCT_SUBCODE">
                                                                    <option value="" disabled="disabled" selected="selected" class="disabled">Product Code 2</option>
                                                                    
                                                                 </select> 
    </form>
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 41788113
sorry. please ignore previous post. and use below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestMe.aspx.cs" Inherits="AppService.TestMe" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
    $(function () {
        $('#PRODUCT_STATE').change(function () {
            $.ajax({
                url: 'TestMe.aspx/GetData',
                data: '{"state":"' + $(this).val() + '"}',
                contentType: "application/json; charset=utf-8",
                type: 'POST',
                dataType: 'JSON'
            }).done(function (resp) {
                var sc = $('#PRODUCT_SUBCODE').empty();
                $.each(resp.d, function (i, d) {
                    sc.append($('<option/>').val(d).html(d));
                });
            });
        });
    });
</script> 
        
         <select   runat="server" class="form-control" id="PRODUCT_STATE" name="PRODUCT_STATE">
                                                                    <option value="" disabled="disabled" selected="selected" class="disabled">Product State</option>   
                                                                    <option value="ND">ND</option>         
                                                                    <option value="OH">OH</option>                                                                                                                 
                                                                    <option value="ZZ">All Other States</option>                                             
                                                                </select>         
 <select runat="server" class="form-control" id="PRODUCT_SUBCODE" name="PRODUCT_SUBCODE">
                                                                    <option value="" disabled="disabled" selected="selected" class="disabled">Product Code 2</option>
                                                                    
                                                                 </select> 
    </form>
</body>
</html>

Open in new window

0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41788199
Are you sure your web method is working - what do you see if you go into the console (F12) - change select 1 - what shows?
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 41788273
Hey! Julian, it works when I put static. Regardless thank you.
0
 
LVL 1

Author Closing Comment

by:ITsolutionWizard
ID: 41788274
Great Helper. Response fast for helps
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41788278
You are welcome
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question