开发者

trying to __dopostback from jscript, RaisePostBackEvent not firing

i'm trying to follow the suggestion here: Call ASP.NET function from JavaScript?

But it's not working for me. The page does post back. but my RaisePostBacKEvent never fires. I am doing this in a usercontrol and not a page:

开发者_如何学运维public partial class MyTreatment : System.Web.UI.UserControl, IPostBackEventHandler

Anyone have any suggestions on how to get this working?

Thanks


Are you specifying the ClientID of your control, as opposed to the ClientID of the page (as in the example from the other SO question you referenced)?

If not then that would explain why the page is posting back but not calling the RaisePostBack method in your control.

To reference the ClientID of your control, call the __doPostBack function like so:

__doPostBack("<%= yourControlID.ClientID %>", "an argument");

As a side note, if your control is the only control on the page then the __doPostBack function will not be created by ASP.NET unless you make a call to GetPostBackEventReference for your control.

You do not necessarily need to make use of the reference but you need to call the method so the page knows to generate the client side function.

You can call GetPostBackEventReference like so:

public class MyTreatment : UserControl, IPostBackEventHandler
{
    protected override void OnLoad(EventArgs e)
    {
        string postBackEventReference = Page.ClientScript.GetPostBackEventReference(this, string.Empty);

        base.OnLoad(e);
    }

    public void RaisePostBackEvent(string eventArgument)
    {

    }
} 

Hope this helps.


It should work with using the user control instance ID's UniqueID property (I couldn't get this to work with ClientID, in my own personal experiences). Like so:

__doPostBack("<%= yourControlID.UniqueID %>", "arg");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜