Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial postback with Javascript

I couldn't find something similar in SO.

In ASP.NET, is there any way that on cue I can cause a partial postback with Javascript in an UpdatePanel?
I tried __doPostBack() but it does a full postback.
I can trick it with a dummy button and fire click() then handle the partial postback that way, but I want a more graceful way than trickery.

Thanks.

Edit: I found this disturbedbuddha.wordpress.com/2007/11/26/… but I can't get it to work =(
I would love for this method to work; it's perfect for me! So far what I can do using this last method is gain reference to the timer. With the timer initially disabled, starting the timer doesn't seem to cause a postback. However, without Ajax, if I simply have the timer enabled initially, it posts back at intervals just fine; why can't the Ajax call cause it?

like image 484
BeemerGuy Avatar asked Oct 19 '10 04:10

BeemerGuy


People also ask

What is partial postback?

Partial postback is a technique where you dont post the whole data to the action page and thus the current page is not changed/removed.This is implemented using ajax where only a limited data is posted back like.

How do you use postback in JavaScript?

How to Raise a Postback from JavaScript? To do this, we need to just call the __doPostBack() function from our javascript code. When the above function is called, it will raise a postback to server.

What is __ Dopostback in JavaScript?

The __doPostBack function contains two arguments, eventTarget and eventArgument. The eventTarget is “Button2” and the eventArgument is “My Argument.” Later, in the C# code behind, I have accessed the eventArgument using the Request. Params collection. The passedArgument variable will contain the value “My Argument.”

What is a postback request?

A postback request is made when user interaction requires a page update, such as when a user clicks on a Save button and triggers a save action.


1 Answers

You can use an AsyncPostBackTrigger with the UpdatePanel to do this. Because you need something that can fire an event, using a button is fairly simple and when hidden works nicely.

If this is your markup:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
    <ContentTemplate>
        <!-- Contents... -->
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ReloadThePanel" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="ReloadThePanel" runat="server" style="display:none;" />

When you want the panel to be updated, you just need to call:

__doPostBack('<%=ReloadThePanel.ClientID %>', null);

This will make ASP.NET think that ReloadThePanel was clicked and the JavaScript auto-generated due to the trigger will handle the rest.

EDIT

You can do a pure JavaScript update of the UpdatePanel without any triggers or hidden buttons. You just need to invoke __doPostBack with the client-side ID as the first argument.

__doPostBack('<%=UpdatePanel1.ClientID %>', null);
like image 163
Matthew Jacobs Avatar answered Oct 17 '22 20:10

Matthew Jacobs