Force forced full postback from UpdatePanel

I have a gridview in the update panel. This is a legacy asp: GridView, so it has a small “export me” icon that does just that. It works by responding to the "export" by clicking the XLS file. The problem is that if you put the smart GridView in the update panel, ASP.NET believes that the .xls file should be written to the panel, which is obviously not what we need. To do this, I need a full postback every time.

My update panels are software generated

Solutions that do not work in this exact scenario (many of which are described in other sections of SO):

  1. In ASP.NET prior to version 4, if you did not specify a control identifier, it will perform a full postback even from the update panel. I have a question only for the latest and best version of .NET 4.

  2. ScriptManager.RegisterPostBackControl looks promising. It forces the control postback to use the correct panel identifier as the target, but otherwise does not help.

  3. Adding PostBackTrigger to the update panel. My update panels are created programmatically, and MS states that this is not supported. My tests show that they are right: I tried everything, but this does not work.

  4. I don’t really like the idea that a smart GridView should break loose, but I tried to make it provide an additional control outside of updatePanel in these circumstances. The idea is for client-side javascript to redirect a client click on my export button inside the panel to simulate clicking that button outside the panel. However, this does not work because I cannot add an "external" control to the page - I get "Cannot change the collection of controls at the DataBind, Init, Load, PreRender or Unload stages". error.

  5. Using jQuery to move the export control outside the panel. MS should have some list of controls that, in their opinion, are located on the panel, and the physical location in the DOM does not matter.

Anyone have any ideas how to make this work? I know that a lot of this should work, but it’s not exactly the same.

+6
source share
4 answers

Solution 3 works for me.

I dynamically add an UpdatePanel to the page, add a Button to the content template, and link. I added a Click event handler in which I update the label (outside of the UpdatePanel ) with the current date / time.

See my test setup below.

Markup

 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:Panel ID="Panel1" runat="server"></asp:Panel> <asp:Label ID="Label1" runat="server"></asp:Label> 

Code for

 protected void Page_Load(object sender, EventArgs e) { string buttonId = "Button1"; UpdatePanel updatePanel = new UpdatePanel(); updatePanel.ID = "UpdatePanel1"; Button button = new Button(); button.ID = "Button1"; button.Text = "Post back"; button.Click += new EventHandler(button_Click); updatePanel.Triggers.Add(new PostBackTrigger() { ControlID = buttonId}); updatePanel.ContentTemplateContainer.Controls.Add(button); Panel1.Controls.Add(updatePanel); } void button_Click(object sender, EventArgs e) { Label1.Text = string.Format("Current date/time: {0}", DateTime.Now.ToString()); } 

Hope this helps.

+4
source

I understand that this was asked a year ago, but if it helps someone else, solution 2 works for me.

 ((ScriptManager)this.Page.Master.FindControl("scrptmgr")).RegisterPostBackControl(lnkbtn); 

The only way to make it work is if the control (in this case lnkbtn) is displayed when the above line of code is executed.

Change After more testing, it now seems that this method only works if there is no feedback between registering the control for full postback and actually clicking on the item. A solution or hack is registering a control for a complete postback every time the page loads.

+3
source

I built a similar inherited GridView control with the ability to export CSV. The way I handled this is to make the export button a simple <a href> current page with the special parameter querystring "?SortablePagableGridViewExportToCSV=XXX" , where XXX is the UniqueID my control. I override the Render method of my control, and there I check the request parameters manually to see if this export parameter has been sent. If so, I write the header of the CSV content type and the response as shown below.

 Me.Page.Response.Clear() Me.Page.Response.ClearContent() Me.Page.Response.ClearHeaders() Me.Page.Response.ContentType = "text/csv" Me.Page.Response.AddHeader("Content-Disposition", "attachment;filename=" & Me.ExportToCsvFileName) Me.Page.Response.End() 

The key is calling Page.Response.End() , which stops further processing. This is a GET request, so I don’t have to worry about the back of UpdatePanel asynch or something like that. The browser behaves fine in this situation, pulling out a download link, but keeping the user on the current page. The browser URL does not change, and the user can even right-click the download link and use Save Target As ... if they wish.

0
source

In case the control that should force the page to return (suppose that the button) is the --inside field of the GridView template, you may have problems setting the controlId for the UpdatePanel trigger ... in this case, you can simply go to to the teemplatefield button and set the "PostBackUrl" property ... the same page!

Thus, by clicking on it, it will force you to completely go back ...

0
source

Source: https://habr.com/ru/post/900450/


All Articles