Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net c#, jquery, webmethod

Posted on 2016-08-09
8
Medium Priority
?
110 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
[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
  • 3
  • 3
8 Comments
 
LVL 19

Expert Comment

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

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 10

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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 41751580
Try it already and does not seem work.
Can u put it in working codes ?
0
 
LVL 10

Accepted Solution

by:
Prakash Samariya earned 2000 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
 
LVL 1

Author Comment

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

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

670 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