Server side method call using __DoPostBack by method name

I want to call a server side method using __DoPostBack and generate HTML, but I donโ€™t want my page to have a hidden ASPA control server side and it can be called a server side method by name rather than control name , call it ??

+1


source to share


4 answers


Whenever I see this question, I get a little nervous that the searcher doesn't quite understand the full implications of triggering a response, so I want to cover them briefly:

  • The browser's current DOM instance has been destroyed. As far as the browser is concerned, you submit the current page and request a complete new one.
  • Not only does the server-side method execute, but the rest of the page's lifecycle, including mandatory data binding or page / page load code. Everything will be remodeled from scratch.


Is this really what you want?

+2


source


Well the same happens if I click the runatserver button all pages will be displayed again.



i called the javascript function __DoPostBack with the name of the control as a parameter and it works the same if i click a button or link.

+1


source


Take a look at the different ways ASP.Net handles AJAX functionality or callbacks for a different term, as it looks like what you are trying to do. The method must exist either within the page, or if it is in a control, then an identifier must exist for that control to determine which button was clicked, since it repeats the same control once on the page.

+1


source


As Joel mentioned, you may have unintended behavior overlapping such writebacks. There is nothing wrong with that if you fully understand what is happening.

I think you really want to be WebMethod

. This will save your page, but will allow you to call a specific method on the server side:

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate()
  {
    return DateTime.Now.ToString();
  }
}

      

Using jQuery to Call ASP.NET AJAX Page Methods Directly

+1


source







All Articles