Click here to Skip to main content
15,879,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

Thanks in advance for your great help..

I have added 2 dropdownlist boxes for Country and State. Based on the slected country, i have to load the states.

I used pagemethod to call the Serverside code of Binding states. But, its not loading..

Can anyone please rectify this.. If any doubt on this, pls ask

I herewith attached my sample code below:

.aspx
-------

XML
<body onload="fnOnload();>
<script type="text/javascript" language="javascript" src="Scripts/Register.js"></script>

    <form runat="server" id="frmMain">
    <div id="reg">
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"  EnablePageMethods="true">



Register.js:
--------------
C#
function LoadState() {
    var CountryId = document.getElementById("ddlICountry").value;alert(CountryId);
    PageMethods.ServerLoadState(CountryId, CallSuccess_LoadState, CallFailed_LoadState);
}
function CallSuccess_LoadState(res) {
    var StateID = new Array();
    var StateName = new Array();
    var Collection = new Array();
    var Collection_ID = new Array();
    var Collection_Text = new Array();
    Collection = res.split('~~~');
    Collection_ID = Collection[0];
    Collection_Text = Collection[1].split('|');
    for (var i = 0; i < Collection_Text.length; i++) {
        StateID[i] = i;
        StateName[i] = Collection_Text[i].split(',');
    }
    document.getElementById("ddlIState").options.length = 0;
    for (var i = 0; i < StateID.length; ++i) {
        addOption(document.getElementById("ddlIState"), StateName[i], StateId[i]);
    }
    document.getElementById("ddlIState").disabled = false;
}
function CallFailed_LoadState(res, destCtrl) {
    alert(res.get_message());
}
function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}


.aspx.cs
-----------

[System.Web.Services.WebMethod]
public static string ServerLoadState(int CountryId)
{
    SqlConnection con = null;
    string Statelist = "";
    string connection = ConfigurationManager.AppSettings["SoftProConnect"];
    con = new SqlConnection(connection);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "sp_GetAllStates";
    cmd.Parameters.AddWithValue("@CountryId", CountryId);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 500;
    cmd.Connection = con;
    if (con.State == 0)
    {
        con.Open();
    }
    Statelist = cmd.ExecuteScalar().ToString();


    return Statelist;
}

Calling the method in dropdownlist:

ddlICountry.Attributes.Add("onChange", "javascript:LoadState();");


[edit]Code blocks tidied up - OriginalGriff[/edit]
Posted
Updated 4-Feb-11 23:19pm
v2
Comments
Sandeep Mewara 5-Feb-11 5:47am    
Did you debugged and see how the execution is happening and if there is any error?
Estys 5-Feb-11 6:33am    
Removed your "Answer" and added it as comment to the Answer of Anupama Roy.
Anupama Roy 6-Feb-11 12:12pm    
As Sandeep has suggested,please debug it & let us know if you run into any errors.I hope know how to debug javascript.If not,do let us know.we can help you on that!

1 solution

I see [System.Web.Services.WebMethod] tag above ServerLoadState.As this is a webmethod,you need to add reference of your asmx in aspx page where you are planning to call it.Please have a look at the below article to understand how webmethods can be called from javascript

http://www.a2zdotnet.com/View.aspx?Id=15[^]
 
Share this answer
 
Comments
Estys 5-Feb-11 6:33am    
from OP :
Thanks for your reply..

But, there is no need to add the webmethod in separate file with .asmx ext.

Pagemethod helps to call the webmethods inside .cs..

Thank you
Sandeep Mewara 5-Feb-11 7:24am    
I think, its ok and thats not needed. What you are suggesting is taking directly to WebService from client using AJAX.

OP is using PageMethod. On high level implementation looks ok, so I asked for his debugging steps and execution but OP ignored my comment totally! Can't help.
Anupama Roy 6-Feb-11 11:56am    
Thank you OP & Sandeep for your feedback!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900