我需要通过向他发送存储在数据库中的二进制文件并更新数据库来处理用户的按钮单击操作,并且使用 ajax 反映在 RadGrid 中的更改。
在执行操作、发送文件和更新网格时,它分别发生得很好,但是当尝试同时执行这两个操作时,它会返回一个错误,指出无法处理响应。
我想知道是否有任何替代方法可以做到这一点。
我有类似的代码:
javascript:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
var pbControl = null;
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance()
.add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
$(".loads").attr("disabled", true);
}
function EndRequestHandler(sender, args) {
if (args.get_error() != undefined) {
var errorMessage = args.get_error().message;
args.set_errorHandled(true);
errorMessage = errorMessage.replace(
"Sys.WebForms.PageRequestManagerServerErrorException:",
"");
alert(errorMessage);
}
$(".loads").attr("disabled", false);
}
</script>
</asp:ScriptManager>
ASP.NET:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="loads">
<telerik:RadGrid ID="Grid" runat="server"
OnItemCommand="Grid_ItemCommand">
<MasterTableView TableLayout="Auto">
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="Button" runat="server"
CommandName="Command"
CommandArgument='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</div>
</ContentTemplate>
</asp:UpdatePanel>
C#:
protected void Grid_ItemCommand(object sender,
Telerik.Web.UI.GridCommandEventArgs e)
{
if(e.CommandName == "Command")
{
// Update database
DatabaseUpdate();
Grid.Rebind();
// Send binary
HttpResponse response = this.Response;
byte[] bin = GetBinary(code);
MemoryStream memStream = null;
memStream = new MemoryStream(bin);
response.Clear();
response.AddHeader("content-disposition",
string.Format("attachment; filename={0}", "file.pdf"));
response.ContentType = string.Format("application/{0}", "pdf");
response.BufferOutput = false;
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
{
response.OutputStream.Write(buffer, 0, bytesRead);
}
response.Flush();
}
}