Click here to Skip to main content
16,013,338 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Getting Error when running javascript on this line
TestWebServic.GetTerritoriesInRegion(item, OnSuccess, OnError)
Microsoft JScript runtime error: Object doesn't support this property or method

C#
// snippet from aspx file

<asp:DropDownList ID="lstRegions" Runat="server" Width="210px"
          DataTextField="label_type"
          DataValueField="label_type_id">
        </asp:DropDownList>


 if (!IsPostBack)
    {
            lstRegions.Attributes.Add("onChange", "GetTerritories('" + lstRegions.ClientID + "')");
    }

//    javascript file
//
var ctrl = null;

function GetTerritories(src) {
   ctrl = document.getElementById(src);

   TestWebServic.GetTerritoriesInRegion(ctrl.options[ctrl.selectedIndex].value, OnSuccess, OnError);
}

function OnSuccess(result) {

}

function OnError(result) {

}
//
// code behind file
//
        [WebMethod]
        public static List<Territory> GetTerritoriesInRegion(string strValue)
        {
        ....do something
        }
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jul-12 16:32pm    
How marking a server side method as [WebMethod] (or any other "trick") can create a JavaScript function, how do you think?
--SA

1 solution

I stared at your code sample in disbelieve for a while and could not get the idea. I still cannot understand your thinking, even when I realized that you have no idea how Web works. Well, the error message must be about your symbol GetTerritoriesInRegion, which apparently does not exist. Perhaps you think that some C# code can mysteriously become a JavaScript method, but how? Only if you generate some HTML + JavaScript code with your C# code and provide it in your Web response, which you could simply put in your aspx file. Do you even understand that JavaScript works on the client part, and C# on the server?

And the attribute [WebMethod] is totally unrelated to this issue. Is can be used to indicate that you want to expose some method by your XML Web service. Please see:
http://msdn.microsoft.com/en-us/library/byxd99hx%28v=vs.90%29.aspx[^].

So, what can I advise? Don't try to "solve" your problem; this is a waste of time and makes no sense. Learn how Web and HTTP work, from the very, very beginning, than get back to ASP.NET.

—SA
 
Share this answer
 
Comments
Member 7969139 24-Jul-12 16:50pm    
Sergey.
Obviously you've never heard of PageMethods. Why don't you read up on it before you open your big mouth.

ScriptManager.EnablePageMethods Property
enhzflep 24-Jul-12 17:19pm    
Since you're clearly 'the expert' on the topic why are you even asking the question?
Given that the error message says 'doesn't support the method or property'
I'd question whether or not it was worth investigating what happens when you try to pass 3 parameters to a function that appears to accept just one...

Definition:
-----------
[WebMethod]
public static List<territory> GetTerritoriesInRegion(string strValue)
{
....do something
}

As used
-------
TestWebServic.GetTerritoriesInRegion(ctrl.options[ctrl.selectedIndex].value, OnSuccess, OnError);


And no, I don't use ASP or ASPX. I know 3/5th and 5/8ths of elephant-all when it comes to that tech. ButI do note the difference in the number of params AND the fact that you've neither showed us the code

<asp:ScriptManager EnablePageMethods="True|False" />

in your snippet, nor assured us you've used it.

Also, do you realize that in the past month SA has racked up 25 times as many points as you've racked up in the past year - that's not counting the remaining days in the month for SA, it's not taking into consideration your freebie sign-up points, nor the extra month on top of the 12 I calculated as your CP membership's age.

To put it another way - SAs contributions are valued at least 300 times what yours are.

And as a last dig - you could have at least posted a clickable link. Fixed it for you..
Member 7969139 24-Jul-12 17:12pm    
You can add static page methods to an ASP.NET page and mark them as Web methods. You can then call these methods from script as if they were part of a Web service, but without creating a separate .asmx file. To create Web methods on a page, import the System.Web.Services namespace and add a WebMethodAttribute attribute to each static method that you want to expose. The methods must be marked public.

For more information, see Exposing Web Services to Client Script.
Sergey Alexandrovich Kryukov 24-Jul-12 18:22pm    
Well, thanks for the hint, but there is no sign of using the Web service in your JavaScript code. Read more careful:
http://msdn.microsoft.com/en-us/library/bb398998%28v=vs.90%29.aspx
If you just hide some essential part of code, this would be a different story...
--SA
Member 7969139 24-Jul-12 22:51pm    
SA. You need to stop degrading people with you mouth. doesn't matter how smart you think you are. you sound like a complete ass to me. You made enough wise remarks in your first post constantly puting down my 'skills'.

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