开发者

A Partial Post Back causes the Full Post Back buttons to act as Partial Post Back, why?

开发者 https://www.devze.com 2023-03-23 14:20 出处:网络
I have a RadGrid control which is created dynamically on page_init and added to a placeholder which is inside an updatePanel on the page.

I have a RadGrid control which is created dynamically on page_init and added to a placeholder which is inside an updatePanel on the page.

I'd need to add a new Button to the CommandItem section of the RadGrid. The button has to support full postback.

RadGrid has an event called RadGrid_ItemCreated() and that's where I've added my new button and it appears on my RadGrid:

 protected virtual void RdGridItemCreated(object sender, GridItemEventArgs e)
    {
        var itemType = e.Item.ItemType;

        switch (itemType)
        {        
            // other cases...
            case GridItemType.CommandItem:
                {
                    var gridCommandItem = e.Item as GridCommandItem;

                    if (gridCommandItem == null) return;

                    if (this.EnablePdfExport)
                    {
                        var pdfButton = CreateExportToPdfButton();

                        PageUtil.RegisterPostBackControl(pdfButton);

                        // this is the cell which contains the export buttons.
                       ((Table)gridCommandItem.Cells[0].Controls[0]).Rows[0].Cells[1].Controls.Add(pdfButton);                                   开发者_开发问答                     
                    }
                    break;
                }
        }
    }

The button has a Click event and a method has been added to it as an event handler:

private Button CreateExportToPdfButton()
    {
        var result = new Button();
        result.ID = "btnExportToPdf";
        result.Click += ExportToPdfButtonClick;
        result.CssClass = "rgExpPDF";
        result.CommandName = "ExportToPdf";
        result.Attributes.Add("title", "Export to Pdf");            
        return result;
    }

To register the postback event for this control I've used the RegisterPostBackControl() method of the ScriptManager.

 public static void RegisterPostBackControl(Control control)
    {
        var currentPage = (Page) HttpContext.Current.CurrentHandler;
        var currentScriptManager = ScriptManager.GetCurrent(currentPage);
        if (currentScriptManager != null)
        {
            currentScriptManager.RegisterPostBackControl(control);
        }
    }

When I click the button on the RadGrid, it posts back to the server but the problem is that its Click event is never raised:

private void ExportToPdfButtonClick(object sender, EventArgs e)
    {
        // process
    }

I don't understand why; any thoughts/help?

If I don't set ID for the button, then the click event is raised but a new problem arises in that case. When a partial postback happens on the page by an external drop downlist to update the radgrid, then my custom export button sends postbacks asynchronously whereas it should post back fully.

Many thanks,


I fixed it by adding the new control in the following event:

 this.RadGrid.MasterTableView.Init += MasterTableViewInit;

 void MasterTableViewInit(object sender, EventArgs e)
    {
        if (!this.EnablePdfExport) return;

        var commandItem = this.RadGrid.MasterTableView.GetItems(GridItemType.CommandItem).SingleOrDefault();

        if (commandItem == null) return;

        AddPdfButton(commandItem as GridCommandItem);
    } 


I am having the same problem. I have tracked it down to Telerik switching the Visible property of the child controls of the RadGrid to false during Render. This only affects partial-page postbacks because Render gets called before the PageRequestManager writes the JavaScript for the postback controls, and it skips controls which are not Visible. For a full postback (or the initial page load), the PageRequestManager writes the JavaScript for the postback controls before the RadGrid is Rendered, and thus the controls are still Visible.

I'm not sure why Telerik is doing this as it causes a lot of problems to muck with the Visible property during the Render stage.

0

精彩评论

暂无评论...
验证码 换一张
取 消