Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__doPostback - calling code behind events from JavaScript?

I wish to call a method in my code behind from JavaScript, I sort of know how to do it.. I must call __DoPostBack passing name of control and parameters..

But what if an event doesn't exist i.e. NO CONTROL. Really what i am trying to do is call an event.. but the event doesn't exist as there is no control associated with it..

I sort of could do this:

If IsPostBack Then
       If Request(”__EVENTTARGET”).Trim() = “CleanMe” Then
           CleanMe()
       End If
.....

But this means I must do it manually. Can I not wire up an event.... otherwise I will have loads of different IFs (i.e. If this passed then call this .. etc..).

Any ideas?

Thanks

like image 475
mark smith Avatar asked Dec 06 '25 15:12

mark smith


2 Answers

You may be able to use a PageMethod to call your codebehind function, here is a link to an example: http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx

like image 71
John Boker Avatar answered Dec 08 '25 03:12

John Boker


If you want to use __doPostBack(), you must have a control to receive the command. However, you don't have to explicitly wire up an event to handle it. If you want the __doPostBack() to invoke, say, Foo(), do the following:

MyControl : IPostBackEventHandler
{
    void RaisePostBackEvent(string eventArgument)
    {
        Foo();
    }
}

Calling __doPostBack() will invoke the RaisePostBackEvent method on the targeted control.

like image 40
Rex M Avatar answered Dec 08 '25 03:12

Rex M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!