1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Using Ajax and the Response.xxxxxx calls

Using Ajax and the Response.xxxxxx calls

7 posts, 0 answered
  1. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    12 Jun 2009
    Link to this post
    I'm pretty new to Ajax, so this might be a problem with an obvious solution, but I'm getting confused.  I'm trying to ajaxify some functionality on a page, and I can't seem to figure out how to get this particular segment to execute (which executes just fine without ajax).  This piece of code runs after a button has been clicked, and generates a CSV file and prompts the user to download it with the browser.

    Response.ContentType = "text/csv"
    Response.AddHeader("content-disposition""attachment; filename=\"" + "MyFile.csv" + "\""); 
    Response.Write(GetCSV()); 
    Response.End(); 

    It seems that Response.Write(...) and Response.End are the specific lines that do not execute.  The button which fires the code above is within a RadAjaxPanel (along with a bunch of other stuff, all of which seems to work fine with Ajax). Is there something special that needs to be done to get the Response.whatever stuff to run with Ajax?
  2. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    15 Jun 2009
    Link to this post
    Btw, I forgot to mentioned that this wasn't a problem when I used ASP.NET's UpdatePanel.  Everything worked fine... The problem is when I replace UpdatePanel with RadAjaxPanel and RadAjaxManager.

    Any thoughts?
  3. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    16 Jun 2009
    Link to this post
    Hi Marko,

    I suppose that you have put the button in RadAjaxPanel. The idea of RadAjaxPanel/UpdatePanel is that they do partial Page updates. When a button is clicked a partial Ajax request is made to the server and changed html portion is returned for the area of the UpdatePanel/RadAjaxPanel. When the button is expected to return file contents (not html partial data) you should not use RadAjaxPanel/Update panel. 

    Can you provide us with a sample code showing how exactly you have used the RadAjaxPanel, so we can easily reproduce the problem on our side.

    Kind regards,
    Parvan
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  4. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    16 Jun 2009
    Link to this post
    Well, you wouldn't really be able to reproduce the behavior as the page interacts with a database and dynamically generates the CSV file.  But I think you're right--I'm probably doing something wrong with the RadAjaxPanel/Manager. 

    Let me back up a step... I think I know what the problem may be related to.  In my working code (using the generic ASP.NET UpdatePanel), I have it set this way:

    <asp:updatepanel runat="server" id="updPanel1"
            <ContentTemplate> 
                <asp:Button ID="btn1" runat="server" Text="button 1" OnClick="btn1_Click" /> 
                <asp:Panel ID="panel1" runat="server"
                    <!-- Some dropdown menus and textboxes go here --> 
                    <!-- along with some SqlDataSource controls --> 
                    <asp:Button ID="btn2" runat="server" OnClick="btn2_Click" Text="Button 2" /> 
                </asp:Panel> 
            </ContentTemplate> 
            <Triggers> 
                <asp:PostBackTrigger ControlID="btn2" /> 
            </Triggers> 
        </asp:updatepanel> 
    <asp:panel id="panel2" runat="server" visible="False"
            <asp:Button ID="btnExportToCsv" runat="server" OnClick="btnExportToCsv_Click" Text="Get CSV" /> 
            <!-- a GridView goes here, along with an SqlDataSource --> 
        </asp:panel> 
    <asp:panel id="panel3" runat="server" visible="False"
            <asp:LinkButton ID="btn3" runat="server" OnClick="btn3_Click">Button 3</asp:LinkButton> 
            <!-- a DetailsView goes here along with a SqlDataSource --> 
    </asp:panel> 
     

    So, because of the nesting of various controls and HTML on the page, I have placed button btn2 inside the UpdatePanel, BUT it really needs it to generate the regular postback (not the asynchronus one), so I use <asp:PostBackTrigger ...> in order to force it to update the entire page and not just the stuff inside the UpdatePanel.  Everything else inside that panel, though, is asynchronous (which is why I need the UpdatePanel to begin with).

    So (taking things one step at a time here), how would I replicate the above behavior using RadAjaxPanel/Manager?  If I simply replace the UpdatePanel with RadAjaxPanel, how would I force button btn2 to do a regular Postback, while other things inside the RadAjaxPanel are asynchronous?

  5. Parvan
    Parvan avatar
    151 posts
    Registered:
    24 Sep 2012
    17 Jun 2009
    Link to this post
    Hi Marko,

    Use the RegisterPostBackControl method of the ScriptManager/RadScriptManager class:
    protected void Page_Load(object sender, EventArgs e) 
        { 
            scriptManager.RegisterPostBackControl( btn2 ); 
        } 

    You may find some more information here about the PostBackTrigger class.

    I hope this information helps you.

    All the best,
    Parvan
    the Telerik team

    Instantly find answers to your questions on the newTelerik Support Portal.
    Check out the tipsfor optimizing your support resource searches.
  6. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    23 Jun 2009
    Link to this post
    Thanks, that worked!  Although, since my script manager is located in the master page, I had to get a reference by calling Page.Master.FindControl("ScriptManager1").  And to make things even more clunky, I have nested master pages, so I had to ultimately do Page.Master.Master.FindControls("ScriptManager1")...   which is really ugly... But hey, it works, and I'm actually fine with it, because in this particular case, it will always be 2 levels of master pages deep...  Not the most elegant solution, but I needed a quick one, this time.  Thanks!
  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    25 Jun 2009
    Link to this post
    Hi Marko,

    We are really glad it is working as expected. There are some things that can be done with the master pages in order to make such implementations easier. Hopefully you will like the PageEditor which we prepare for version 4.0 ;)
    Let us know if you need anything else.

    Sincerely yours,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
7 posts, 0 answered