0

I want to call a c# function from JavaScript. I tried the solution specified in Call ASP.NET function from JavaScript? but the RaisePostBackEvent is not getting called.

My JavaScript is:

<script>
    function link_load() {
        var pageId = '<%=  Page.ClientID %>';
        __doPostBack(pageId, argumentString);
        document.getElementById("sidebar1").innerHTML = "working";
    }
</script>

My C# code is:

public void RaisePostBackEvent(string eventArgument)
{
    sidebar1.InnerHtml = "working inside";
}

The JavaScript code is executed, which I verified.

Community
  • 1
  • 1
Srinivas
  • 332
  • 4
  • 18
  • Have you tried changing a hidden field's value from javascript and postback, then check the hidden value in c#'s pageload function ? – Emre Acar Sep 27 '13 at 05:36
  • Are you inherit "IPostBackEventHandler" interface in you page ? – Sain Pradeep Sep 27 '13 at 05:39
  • tackoverflow.com/questions/3713/call-asp-net-function-from-javascript –  Sep 27 '13 at 05:45
  • @EmreAcar Can you explain, please ? I do not understand what you suppose. – Srinivas Sep 27 '13 at 05:50
  • do you have an event handler assigned that references the RaisePostBackEvent method? – ps2goat Sep 27 '13 at 05:50
  • @ShahroozJefriㇱ I have added the link in my question. I'm not able to call the function still. – Srinivas Sep 27 '13 at 05:51
  • @ps2goat I haven't. Can you please explain further ? – Srinivas Sep 27 '13 at 05:51
  • never mind. It may have to do with the order of eventing. When you post back with web forms, the entire lifecycle executes. The assignment you are doing (`sidebar1.InnerHtml = "working inside"`) happens early in the cycle. In the example here: http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2, they are raising the click event so the code actually executes when it should, which is after Page_Load. You should perhaps ensure the code is executing in the expected order and let us know. – ps2goat Sep 27 '13 at 05:58

1 Answers1

0
[webmethod]
public void RaisePostBackEvent(string eventArgument)
{
    sidebar1.InnerHtml = "working inside";
}

in javascriptenter code here

<script type="javascript">
 $.ajax({
     url:'pagename.aspx/RaisePostBackEvent'`enter code here`
      enter code here

  });
</script>
Pratik
  • 1,472
  • 7
  • 20
  • 36
krish
  • 19
  • 1
  • 4