Solved

asp.net c#, jquery, webmethod

Posted on 2016-08-09
8
62 Views
Last Modified: 2016-10-09
I have jquery and I try to map with webmethod written in c#. but do not know what to do....with dictionary c#.
Please helps

<select  id="PRODUCT_LIST" name="PRODUCT_LIST"> should show up
like
<option value="1">1000</option>
<option value="2">2000</option>

Thanks,



<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Tester5.aspx.cs" Inherits="AppService.Tester5" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<select onchange="ShowList()" runat="server" required class="form-control" id="PRODUCT_CODE" name="PRODUCT_CODE">
                                                                    <option value="" disabled="disabled" selected="selected" class="disabled">Product Code</option>  
                                                                    <option value="13081">13081</option>  
                                                                    <option value="13082">13082</option>                                            
                                                                </select>                                                                                      
<br />
<select  onchange="ShowList()"  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 Others</option>                                            
                                                                </select>          
<br />  
<select style="display:none;"  runat="server" required class="form-control" id="PRODUCT_LIST" name="PRODUCT_LIST"></select>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
    function ShowList() {
            $.ajax({
                type: "POST",
                url: "Tester5.aspx/GetList",
                data: '', //?
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        //?
     }
</script>
</asp:Content>

//code behind:

[System.Web.Services.WebMethod]
        public static Dictionary<string,string>GetList(string PRODUCT_CODE, string PRODUCT_STATE)
        {
            Dictionary<string,string> s = new Dictionary<string,string>();
            s.Add("1", "1000");
            s.Add("2", "2000");
            return s;
        }
0
Comment
Question by:ITsolutionWizard
  • 3
  • 3
8 Comments
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 41749946
What you need to do exactly ??
0
 

Author Comment

by:ITsolutionWizard
ID: 41750960
I need below shown.

<select  id="PRODUCT_LIST" name="PRODUCT_LIST"> should show up
like
<option value="1">1000</option>
<option value="2">2000</option>
0
 
LVL 9

Expert Comment

by:Prakash Samariya
ID: 41751577
Wild guess, logic is something like below, you need to check what you get in response object from OnSuccess function and need to modify bit of code below
function OnSuccess(response) {
        $.each(msg.d, function(index, item) {
                    $('#PRODUCT_LIST').append($('<option />').val(this['Key']).text(this['Value']));
        });
}

Open in new window

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: 41751580
Try it already and does not seem work.
Can u put it in working codes ?
0
 
LVL 9

Accepted Solution

by:
Prakash Samariya earned 500 total points
ID: 41751689
Please find the working code!
snapshot_drpddict.png
In Web method
//[System.Web.Services.WebMethod]
public string GetList()
{
	Dictionary<string, string> s = new Dictionary<string, string>();
	s.Add("1", "1000");
	s.Add("2", "2000");

	//Need to modify dictionary to JSON format
	var kvs = s.Select(kvp => string.Format("\"{0}\":\"{1}\"", kvp.Key, string.Concat("", kvp.Value)));
	return string.Concat("{", string.Join(",", kvs), "}");
}

Open in new window


In script
<script type="text/javascript">
    function ShowList() {
            $.ajax({
                type: "GET",
                url: "Home/GetList",
                data: '', //?
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        $.each(response, function (index, item) {
            $('#PRODUCT_LIST').append($('<option />').val(index).text(item));
        });
        $('#PRODUCT_LIST').show();
    }
</script>

Open in new window

0
 

Author Comment

by:ITsolutionWizard
ID: 41752565
Thanks if we now use wcf ....what do we need to change ?
0
 
LVL 9

Expert Comment

by:Prakash Samariya
ID: 41753388
I guess, I have answered to your question with working code!

Note: Please ask other question in separate thread to get better answer!

However,
Answer: you need to consume wcf service in your webmethod and then, (I think) you know what to do in webmethod, right?
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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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)

840 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