首页 > 解决方案 > 为什么在用户控件中的一组指令后 RegisterClientScriptBlock 不起作用

问题描述

我的目标是使用一个按钮(用户控件)创建 asp.net 网络表单页面,该页面应该创建包含一些信息的 .xlsx 文件,并且在创建文件后应该发送有关成功的警报,或者警报说发生了错误。当我的方法只包含

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);

一切正常,点击按钮后网站显示警报,但是当我像这样插入代码时

 protected void createXlsx(object sender, EventArgs e)
        {
            using (var workbook = new XLWorkbook())
            {
                string a = "A";
                int b = 1;
                string c = String.Concat(a.ToString(), b);
                var worksheet = workbook.Worksheets.Add("Sample Sheet");
                worksheet.Cell(c).Value = "Id";
                worksheet.Cell("B1").Value = "Imie Nazwisko";
                worksheet.Cell("C1").Value = "Kwota";
                worksheet.Cell("A2").Value = "";


                // Prepare the response
                HttpResponse httpResponse = Response;
                httpResponse.Clear();
                httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                httpResponse.AddHeader("content-disposition", "attachment;filename=\"HelloWorld.xlsx\"");

                // Flush the workbook to the Response.OutputStream
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    workbook.SaveAs(memoryStream);
                    memoryStream.WriteTo(httpResponse.OutputStream);
                    memoryStream.Close();
                }

            }
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Created file succesfully')", true);
}

该站点下载 xmlx 文件,但单击按钮后不显示警报。

您对如何解决此问题或以其他方式显示消息有任何想法吗?

标签: c#asp.netwebforms

解决方案


推荐阅读