在radgrid上实现PDF生成后单击按钮时出错

时间:2011-07-28 08:22:55

标签: telerik-grid

我的要求是在点击网格中的图像按钮时生成PDF。在同一页面中,我有一个搜索按钮(btnSearch)。我已经实现了OnNeedDataSource事件。点击搜索按钮我想重新绑定radgrid但是我得到这个errorerror“ErrorDetails:System.ArgumentException:无法取消注册带有ID'upnlPdf'的UpdatePanel,因为它没有在ScriptManager中注册。如果UpdatePanel已从控制树中删除并稍后再次添加(可能不受支持),则可能会发生这种情况。参数名:UpdatePanel的在System.Web.UI.PageRequestManager.UnregisterUpdatePanel(UpdatePanel中的UpdatePanel)在System.Web.UI.ScriptManager.System.Web.UI.IScriptManagerInternal.UnregisterUpdatePanel(UpdatePanel中的UpdatePanel)在System.Web.UI.UpdatePanel.OnUnload (EventArgs e)上System.Web.UI.Control.UnloadRecursive(布尔处置)在System.Web.UI.Control.UnloadRecursive(布尔处置)在System.Web.UI.Control.UnloadRecursive(布尔处置)在的System.Web .UI.Control.UnloadRecursive(布尔处置)在System.Web.UI.Control.RemovedControl(控制控制)在System.Web.UI.ControlCollection.RemoveAt(的Int32指数)在System.Web.UI.ControlCollection.Clear()在System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable的数据)在System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable的数据)在System.Web.UI.DataSourceView.Select(DataSourceSelectArguments参数,DataSourceViewSelectCallback回调)在System.Web.UI.WebControls.Dat aBoundControl.PerformSelect()在Telerik.Web.UI.GridTableView.PerformSelect()在System.Web.UI.WebControls.BaseDataBoundControl.DataBind()在Telerik.Web.UI.GridTableView.DataBind()在Telerik.Web.UI。 RadGrid.DataBind()在Telerik.Web.UI.RadGrid.AutoDataBind(GridRebindReason rebindReason)在Telerik.Web.UI.RadGrid.Rebind()在btnSearch_Click(对象发件人,EventArgs的)”。

在aspx页面

<%@ Page Language="C#" MasterPageFile="~/ATMaster.Master" AutoEventWireup="true">
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel runat="server" ID="upnlSecurityDoc" UpdateMode="Conditional">
        <ContentTemplate>
<telerik:RadGrid ID="RadGrid1" runat="server" Width="1050px" TabIndex="5" Height="350px"
                                        AutoGenerateColumns="False" ForeColor="#333333" PageSize="10" AllowFilteringByColumn="false"
                                        Skin="Outlook" AllowPaging="true" OnNeedDataSource=" RadGrid1_NeedDataSource"
                                        BorderWidth="1px" BorderColor="#cccccc" OnItemCommand=" RadGrid1_ItemCommand"
                                        >
                                        <HeaderContextMenu EnableAutoScroll="True">
                                        </HeaderContextMenu>
                                        <GroupingSettings CaseSensitive="false" />
                                        <MasterTableView PagerStyle-AlwaysVisible="true">
                                            <CommandItemSettings ExportToPdfText="Export to Pdf" />
                                            <Columns>
                                                <telerik:GridTemplateColumn HeaderText="SlNo" AllowFiltering="false">
                                                    <HeaderStyle HorizontalAlign="Left" Font-Bold="true" Width="50px" />
                                                    <ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
                                                    <ItemTemplate>
                                                        <%# this.gvPendingOrder.CurrentPageIndex * this.gvPendingOrder.PageSize + Container.ItemIndex + 1%>
                                                    </ItemTemplate>
                                                </telerik:GridTemplateColumn>

<telerik:GridTemplateColumn HeaderText="PDF">
                                                    <HeaderStyle Width="50px" />
                                                    <ItemStyle Width="50px" />
                                                    <ItemTemplate>
                                                        <asp:UpdatePanel runat="server" ID="upnlPdf">
                                                            <ContentTemplate>
                                                                <asp:ImageButton runat="server" ID="imgbtnPdf" AlternateText="Security Document Print"
                                                                    CommandArgument='<%# Eval("OrderId") %>' ImageUrl="../Images/pdf_icon.gif" ImageAlign="Middle"
                                                                    CommandName="Generate" ToolTip='<%# "Document" + Eval("OrderNo") + "." %>' />
                                                            </ContentTemplate>
                                                            <Triggers>
                                                                <asp:PostBackTrigger ControlID="imgbtnPdf" />                                                            </Triggers>
                                                        </asp:UpdatePanel>
                                                    </ItemTemplate>
                                                </telerik:GridTemplateColumn>
                                            </Columns>
                                        </MasterTableView>
                                        <ClientSettings EnableRowHoverStyle="true">
                                            <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True">
                                            </Scrolling>
                                        </ClientSettings>
                                    </telerik:RadGrid>
</ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

在代码背后

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Generate")
                {
                    string strOrderId = string.Empty;
                    strOrderId = e.CommandArgument.ToString();
                    LocalReport localReport = new LocalReport();
                localReport.ReportPath = Server.MapPath("~/Reports/report1.rdlc");
                localReport.EnableHyperlinks = true;
                localReport.EnableExternalImages = true;

                DataSet dataset1 = new DataSet();
                DataTable datatable1 =GetDetails(strOrderId);
                datatable1.TableName = "Document";
                dataset1.Tables.Add(datatable1.Copy());

                ReportParameter[] @params = new ReportParameter[4];
                localReport.SetParameters(@params);

                //A method that returns a collection for our report Note: A report can have multiple data sources 
                localReport.DataSources.Add(new ReportDataSource("dataset", dataset1.Tables[0]));
                string reportType = "pdf";
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string fileNameExtension = string.Empty;
                string deviceInfo = "<DeviceInfo>" + " <OutputFormat>pdf</OutputFormat>" + "</DeviceInfo>";
                Warning[] warnings = null;
                string[] streams = null;
                byte[] renderedBytes = null;

                //Render the report 
                renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

                //Clear the response stream and write the bytes to the outputstream 
                //Set content-disposition to "attachment" so that user is prompted to take an action on the file (open or save) 
                Response.Clear();
                Response.ContentType = mimeType;
                strOrderId = "Doc" + strOrderId;
                Response.AddHeader("content-disposition", ("attachment; filename=" + strOrderId + ".pdf"));
                Response.BinaryWrite(renderedBytes);
                Response.End();                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = objUtl.GetErrorMessage(ex, this);
                lblMessage.Visible = true;
            }
        }

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但不知何故,UpdatePanel从ScriptManger中注销了两次(它也发生在RadGrid.Rebind()方法中;我遇到的情况),第二次从ScriptManger取消注册“无法取消注册UpdatePanel ...”错误。 解决方法是使用反射在两个取消注册事件之间的某个位置注册UpdatePanel和ScriptManger,如下所示:

protected void UpdatePanel_Unload(object sender, EventArgs e) { MethodInfo methodInfo = typeof(ScriptManager).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) .Where(i => i.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")).First(); methodInfo.Invoke(ScriptManager.GetCurrent(Page), new object[] { sender as UpdatePanel }); }

您应该将UpdatePanel_Unload添加到UpdatePanel的OnUnload事件:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnUnload="UpdatePanel_Unload">

这个解决方案解决了我的问题,我希望它对你也有效。

您可以找到问题的完整详细信息here

此致 阿里