locked
Response is not available in this context. RRS feed

  • Question

  • User736276958 posted

    hi all

    i am trying to call server side method on client side because on linkbutton click page is refreshing 

    To avoid page refresh i  am calling server side method on client side

    but i am getting error as "Httpexception has caught Response is not available in this context."

    my code is

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnableScriptGlobalization="true">

    </asp:ScriptManager>


    <asp:LinkButton ID="ibtncommonzoom" runat="server" class="mdi-action-find-in-page" Width="25%" Height="25%" OnClientClick="ibtncommonzoom_Click()" ></asp:LinkButton>

    <script type="text/javascript">

    function ibtncommonzoom_Click()
    {
    var queryID = document.getElementById('<%=Label1.ClientID %>').innerText;
    var name = ddlcommon.options[ddlcommon.selectedIndex].value;
    var area = null;
    var no = null;
    if (name != "0") {
    PageMethods.mes(queryID, name, area, no);
    }
    return false;
    }
    </script>

    aspx.cs

    static developing instance = null;

    public developing()
    {
    instance = this;

    }

    public static void mes(string queryID, string name, string indusarea, string plotno)
    {

    instance.zoomlayers(queryID, name, indusarea, plotno);
    }

    [ScriptMethod, WebMethod]

    public void layers(string queryID, string name, string area, string no)
    {

    try
    {

    ds = objServiceClientobjService.names(queryID, name, indusarea, plotno);

    .

    .

    .

    .

    .
    ////////////////////////////////////

    String locateScript = String.Format("<script language='javascript'>Test('{0}','{1}','{2}','{3}')</script>", valx1, valy1, valx2, valy2);------------not going to this function
    Locate(locateScript);

    }

    protected string Locate(string locateScript)
    {

    Response.Write(locateScript);
    return (locateScript);
    }

    i have wrote public static void mes(----------------) and calling public void layers(--------------------) because when i mention public static void layers(--------------)

    then inside the method dataset(wcf) and controls are getting error as "An object reference is required for non static field, method----"

    thatwhy i have wrote public static void mes(----------------) 

    every thing is working fine , it is goning to wcf and getting the values but it is not going to external js page Test(x1,y1,x2,y2) function

    and getting error as "Httpexception has caught Response is not available in this context.'

    Tuesday, December 22, 2015 5:58 AM

Answers

  • User1724605321 posted

    Hi  rishe28,

    how to return multiple values in public void method

    You could make a ajax call to fire server side function , if you want to get multiple values in client side , code below is for your reference :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            function ShowCurrentTime() {
                $.ajax({
                    type: "POST",
                    url: "WebForm3.aspx/GetCurrentTime",
                    data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            }
            function OnSuccess(response) {
                $('#divDescription').css('visibility', 'visible');
                $('#lblName').html(response.d[0]);
                $('#lblTime').html(response.d[1]);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div>
                Your Name :
                <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                <input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()" />
            </div>
            <br />
            <hr />
            <div id="divDescription" style="visibility: hidden;">
                Hello <span id="lblName"></span>
                <br />
                The Current Time is: <span id="lblTime"></span>
            </div>
        </div>
        </form>
    </body>
    </html>
    

    Code behind :

            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            [System.Web.Services.WebMethod]
            public static string[] GetCurrentTime(string name)
            {
                string description = name + "," + DateTime.Now.ToString();
                string[] a = description.Split(',');
                return a;
            }

    You could also refer to below articles for details :

    http://api.jquery.com/jquery.ajax/

    http://weblogs.asp.net/karan/calling-server-side-method-using-jquery-ajax

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 22, 2015 7:25 AM

All replies

  • User1083584480 posted

    hi,

    You need to return service value from server to client by a method and then call your javascript method with those values. it means on linkbutton click a page method will call that will return data. you can use ajax here to call method then call your test method with returned values.

    Tuesday, December 22, 2015 6:28 AM
  • User736276958 posted

    please tell me the code for

    how to return multiple values in public void method

    Tuesday, December 22, 2015 6:31 AM
  • User1724605321 posted

    Hi  rishe28,

    how to return multiple values in public void method

    You could make a ajax call to fire server side function , if you want to get multiple values in client side , code below is for your reference :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            function ShowCurrentTime() {
                $.ajax({
                    type: "POST",
                    url: "WebForm3.aspx/GetCurrentTime",
                    data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                    }
                });
            }
            function OnSuccess(response) {
                $('#divDescription').css('visibility', 'visible');
                $('#lblName').html(response.d[0]);
                $('#lblTime').html(response.d[1]);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div>
                Your Name :
                <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
                <input id="btnGetTime" type="button" value="Show Current Time" onclick="ShowCurrentTime()" />
            </div>
            <br />
            <hr />
            <div id="divDescription" style="visibility: hidden;">
                Hello <span id="lblName"></span>
                <br />
                The Current Time is: <span id="lblTime"></span>
            </div>
        </div>
        </form>
    </body>
    </html>
    

    Code behind :

            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            [System.Web.Services.WebMethod]
            public static string[] GetCurrentTime(string name)
            {
                string description = name + "," + DateTime.Now.ToString();
                string[] a = description.Split(',');
                return a;
            }

    You could also refer to below articles for details :

    http://api.jquery.com/jquery.ajax/

    http://weblogs.asp.net/karan/calling-server-side-method-using-jquery-ajax

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 22, 2015 7:25 AM