Cristian Boariu
Cristian Boariu

Reputation: 9621

asp.net - do AsyncPostBack using js

I've spent almost 3 hours for the following case:

I've tried a lot of situations but I am unable to find a clear example.

I have an update panel and when I am pressing a specific image button inside it, some popup is displayed. (the popup is hidden initially, and when the request is done, the display:none is removed=> it appears fine)

 function BeginRequestHandler(sender, args) {
    }
    function EndRequestHandler(sender, args) {
        document.getElementById('popup').style.display = '';
        document.getElementById('overlay').style.display = '';

    }

Well, the issue is that I need to make a js function to do the asyncPostBack because this js function will be called from a flash control when doing click on some portion of that control, so I want to simulate my click on the image. (because doing click this popup is displayed as you see above)

The issue is that I am not able to find anywhere such an example. This js function will receive a parameter so when I do the postback I want to be able to get that parameter on the server side. (this parameters seems to be the main problem =- how I send it to the server when I am doing postback?).

Sorry if I was not very clear, but can you give me some documentation for this or example?

Thanks a lot!

UPDATE: Please note that the first case I've already done, using an ImageButton in my UpdatePanel:

<asp:ImageButton ID="lnkDetails" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Logo") %>'
                            AlternateText='<%# DataBinder.Eval(Container.DataItem, "Name") %>' OnCommand="lnkDetails_Command"
                            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "TenantID") %>' Text="Click">
                        </asp:ImageButton></li>

What I only need is to "simulate" this click, using a javascript to make the async post back (because when I will call from flash, flash control will call my js method for postback)

Upvotes: 5

Views: 4476

Answers (2)

James Johnson
James Johnson

Reputation: 46047

This should work quite well:

<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick='return doSomething(<%#Eval("SomeValue")%>);' />

EDIT: Try wrapping your JavaScript function with this:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function () {
    doSomething = function(argument){
        __doPostBack("<%=ImageButton1.ClientID%>", argument);
        return true; //don't know if you need this
    }
});

EDIT: Check your ScriptManager and make sure that EnablePartialRendering is set to true.

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" ... />

In the code-behind:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    //call the RaisePostBack event 
    base.RaisePostBackEvent(source, eventArgument);

    if (source == ImageButton1)
    {
        string arg = eventArgument;
    }
}

Upvotes: 1

Jamie Treworgy
Jamie Treworgy

Reputation: 24334

To initiate an async postback from javascript:

__doPostBack(target,args)

target is the UniqueID of the UpdatePanel you want to target. Usually it works fine to just pass an empty string (will refresh all auto-updating panels).

The 2nd parameter is arguments that you can use for whatever you want.

Both will be available to you on the server:

Request.Form["__EVENTTARGET"]
Request.Form["__EVENTARGUMENT"]

Upvotes: 2

Related Questions