__doPostback - 从JavaScript调用事件后面的代码?

时间:2022-08-25 23:02:33

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..

我希望从我的代码中调用一个方法,我知道如何做到这一点..我必须调用__DoPostBack传递控件和参数的名称..

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..).

但这意味着我必须手动完成。我可以不连接一个事件....否则我将有大量不同的IF(即如果这传递,然后调用此..等...)。

Any ideas?

Thanks

3 个解决方案

#1


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

您可以使用PageMethod来调用您的代码隐藏功能,这里有一个示例的链接:http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net- Ajax的pagemethods.aspx

#2


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:

如果要使用__doPostBack(),则必须具有控件才能接收命令。但是,您不必明确连接事件来处理它。如果你想让__doPostBack()调用,比如Foo(),请执行以下操作:

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

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

调用__doPostBack()将调用目标控件上的RaisePostBackEvent方法。

#3


If you realy want to wire up to an event, the option that will give you less trouble is to create a hidden asp button and click it via javascript

如果您真的想要连接到一个事件,那么会给您带来更少麻烦的选项是创建一个隐藏的asp按钮并通过javascript单击它

#1


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

您可以使用PageMethod来调用您的代码隐藏功能,这里有一个示例的链接:http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net- Ajax的pagemethods.aspx

#2


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:

如果要使用__doPostBack(),则必须具有控件才能接收命令。但是,您不必明确连接事件来处理它。如果你想让__doPostBack()调用,比如Foo(),请执行以下操作:

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

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

调用__doPostBack()将调用目标控件上的RaisePostBackEvent方法。

#3


If you realy want to wire up to an event, the option that will give you less trouble is to create a hidden asp button and click it via javascript

如果您真的想要连接到一个事件,那么会给您带来更少麻烦的选项是创建一个隐藏的asp按钮并通过javascript单击它

相关文章