Solved

jquery, aspx

Posted on 2016-09-07
10
57 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
  • 6
  • 3
10 Comments
 
LVL 55

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
 

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
 

Author Comment

by:ITsolutionWizard
ID: 41788092
the codes do not seem working
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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 55

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
 

Author Comment

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

Author Closing Comment

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

Expert Comment

by:Julian Hansen
ID: 41788278
You are welcome
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
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…

839 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