Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
I was trying to call code behind function using JQUERY.
I gives an error:
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class

I paste down my code it might give you hint, where I'm going wrong

Client Side:
$("a[id$='lnkQC']").click(function() {
	                $.ajax({
	                    type: "POST",
	                    url: "Production_Iteration_myjob_view.aspx/fillRepeater",
	                    data: "{}",
	                    contentType: "application/json; charset=utf-8",
	                    dataType: "json",
	                    success: function(data) { 
	                        if (data.d == 1)
	                            alert("Successful");
	                        else
	                            alert("Error");
	                    }
	                });
	            });


Code Behind:
<system.web.services.webmethod()> _
   Public Shared Function fillRepeater() As Integer
       Dim objDT As New DataTable
       Try
           objDT = AliaDAO.clsQualitycheck_DAO.getFilterDailyDetails(2, intIterationid)
           'Create the object of PagedDataSource
           Dim objPDS As New PagedDataSource
           'Assign our data source to PagedDataSource object
           objPDS.DataSource = objDT.DefaultView
           'Set the allow paging to true
           objPDS.AllowPaging = True
           'Set the number of items you want to show
           objPDS.PageSize = 10
           'Disable First, Prev, Next, Last buttons
           btnPrevious.Enabled = Not objPDS.IsFirstPage
           btnNext.Enabled = Not objPDS.IsLastPage
           btnFirst.Enabled = Not objPDS.IsFirstPage
           btnLast.Enabled = Not objPDS.IsLastPage
           'Assign PagedDataSource to repeater
           rptQC.DataSource = objPDS
           rptQC.DataBind()
           'Change the text Now viewing text
           lblCurrentPage.Text = "Now viewing : " & (NowViewing + 1).ToString() & " of " & objPDS.PageCount.ToString()
       Catch ex As Exception
           Return -1
       End Try
       Return 1
   End Function


It gives error for intIterationid, btnPrevious, btnNext, btnFirst, btnLastrptQC, lblCurrentPage

intIterationid is local variable
and rest are controls


This error is because I have declared function shared, but until I declare shared I cant call this function using JQuery.

Any hint??

Thanks in Advance
Posted

1 solution

Seems issue with URL, Please refer:

var loc = window.location.href;
loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
$.ajax({
         type: "POST",
         url: loc + "/" + methodName,
         data: "{" + args + "}",


Ref: http://weblogs.asp.net/craigshoemaker/archive/2008/11/07/using-jquery-to-call-asp-net-ajax-page-methods-by-example.aspx[^]
 
Share this answer
 
Comments
dhage.prashant01 26-Jul-11 5:22am    
As per my knowledge, static function can only refer static members.
So due to this there must be error.
I'm not sure.
Parwej Ahamad 26-Jul-11 5:26am    
Yes it's true, mean you are using web controls inside this method cause the error?
dhage.prashant01 26-Jul-11 5:53am    
but how can i call fillRepeater function using JQuery?
Why do function to be declare SHARED to be called from client side?
Parwej Ahamad 26-Jul-11 5:58am    
Because you are not creating object of the current page just calling directly through the method name and this is the way only possible. So if you know, we can call all static method directly by the class name without instantiate.
As per your requirement, I guess you don't want to postback your page so you are trying to use Jquery with Ajax. Why you didn't use Update Panel in this situation?
dhage.prashant01 26-Jul-11 6:05am    
No worry, I have replaced HyperLink with LinkButton, now the problem is LinkButton click event is not getting fired.

I tried following:
<asp:LinkButton runat="server" id="lnkQC" href="#dialog" name="modal" OnClick="lnkQC_Click">View

Protected Sub lnkQC_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkQC.Click
fillRepeater()
End Sub

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