1

I use an UpdatePanel and UpdateProgress.

Both Response.Redirect and JavaScript redirect window not working with them.

This code is working:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DynamicLayout="true"
        AssociatedUpdatePanelID="UpdatePanel1">
    <ProgressTemplate>
        <asp:Image ID="ImageWait" runat="server" ImageUrl="../images/wait.gif" />
    </ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="ButtonSave" runat="server" Text="Save"
        OnClick="ButtonSave_Click" ValidationGroup="Valid1" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ButtonSave"
            EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

Not working :

protected void ButtonSave_Click(object sender, EventArgs e)
{
    // Saved here

    Response.Redirect("../Main/Success.aspx");
}

Not working, too :

protected void ButtonSave_Click(object sender, EventArgs e)
{
    // Saved here

    Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect",
        "location.href = '../Main/Success.aspx'", true);
}

How can I fix this ?

Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
  • if you put a break point in the event handler does it reach it? I am guessing, not? – DaniDev Oct 09 '17 at 22:00
  • Possible duplicate of [How can I run some javascript after an update panel refreshes?](https://stackoverflow.com/questions/1190549/how-can-i-run-some-javascript-after-an-update-panel-refreshes) – hardkoded Oct 09 '17 at 22:23
  • @DaniDev yes, the compiler reaches the button line but the redirect not executed – Bengi Besçeli Oct 09 '17 at 22:33
  • 1
    Async calls with redirects are not a good/safe practice because you are creating another thread which will be left orphaned when redirecting. If you are going redirect you don't need to create a trigger for it since it will not be updating the form. I recommend you just do a simple postback – DaniDev Oct 09 '17 at 22:56
  • @DaniDev, okey thanks, so you say, async. calls may cause to injection, is it right? – Bengi Besçeli Oct 10 '17 at 09:23
  • @DaniDev, how can I do a simple postback? If I refresh the same page, it does not work, too. – Bengi Besçeli Oct 10 '17 at 11:44
  • a simple postback means that you user the event handler ("ButtonSave_Click") as you are and just remove the async trigger (the trigger is used to tell the form that it needs to update the Update Panel. BUT you are not updating the form, as you are redirecting. – DaniDev Oct 10 '17 at 16:06
  • You have already accepted an answer but I am concerned that you are creating a problem for yourself which will manifest when you introduce this into production. If you wish I will post an answer which describes and explains what I am saying – DaniDev Oct 10 '17 at 16:09
  • Yes I wanted to use a more secure way. I will ask a new question – Bengi Besçeli Oct 10 '17 at 16:27
  • You don't really need a new question. I can just post an answer here if you like. Basically you just need to remove the Trigger and either one of your options above should work. – DaniDev Oct 10 '17 at 16:31
  • Yes please. It would be great – Bengi Besçeli Oct 10 '17 at 16:45
  • where are the contents/ input controls that you are saving. Are there any other controls/triggers that trigger the Ajax functionality for your UpdatePanel ? – DaniDev Oct 10 '17 at 17:51
  • but they are so long – Bengi Besçeli Oct 10 '17 at 21:29

1 Answers1

0

You can use the ScriptManager

protected void redirector_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "redirect",
        "location.href = 'ListViewDemo.aspx';", true);
}
hardkoded
  • 18,915
  • 3
  • 52
  • 64