Solved

asp.net c#, jquery, webmethod

Posted on 2016-08-09
8
43 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 8

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

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

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 8

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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…
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…

786 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