【问题标题】:code behind changes are lost after executing response.appendheader content-disposition执行 response.appendheader content-disposition 后更改后面的代码丢失
【发布时间】:2017-04-11 23:25:20
【问题描述】:

在 asp:LinkBut​​ton 点击​​事件中执行 response.appendheader content-disposition 后,更改背后的代码会丢失。

我有一个 asp:LinkBut​​ton,当用户点击时,附件将被下载,我还需要知道链接是否被点击。 出于测试目的,我放置了一个文本框并在 asp:LinkBut​​ton 单击事件上更改了它的文本。 但文本框文本仍然是旧文本并且文件正在正确下载。

<asp:LinkButton runat="server" ID="LinkAttachmentView" 
                ToolTip="Click here to Download  the Attachment" Font-Bold="True" 
                ForeColor="Red"  >Click here to View  the Attachment
 </asp:LinkButton>

这里是代码

 Protected Sub LinkAttachmentView_Click(sender As Object, e As System.EventArgs) Handles LinkAttachmentView.Click

        If HdAttachmentFileName.Value = "" Then
            HdAttachmentClicked.Value = "0"
            Return
        End If
        HdAttachmentClicked.Value = "1"
        TextBox1.Text = "clicked"

        Response.ContentType = ContentType
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(HdAttachmentFileName.Value))
        Response.WriteFile(HdAttachmentFileName.Value)

        Response.Flush()

        Response.End()

        HdAttachmentClicked.Value = "1"
        TextBox1.Text = "clicked"

    End Sub

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    我只需要知道用户是否点击了链接按钮。我只是在链接按钮客户端单击上添加了一个 javascript 函数来更新隐藏字段的值。

    这里是javascript:

     function SetClickValue() {
            var HdField = document.getElementById("<%=HdAttachmentClicked.ClientID%>");
            HdField.value = "1"; 
        }
    

    HTML:

    <asp:LinkButton runat="server" ID="LinkAttachmentView" 
                    ToolTip="Click here to Download  the Attachment" Font-Bold="True" 
                    ForeColor="Red" onclientclick="SetClickValue()"               
                    >Click here to View  the Attachment
                    </asp:LinkButton>
    

    点击事件现在只用于下载附件:

    Protected Sub LinkAttachmentView_Click(sender As Object, e As System.EventArgs) Handles LinkAttachmentView.Click
    
            If HdAttachmentFileName.Value = "" Then
                HdAttachmentClicked.Value = "0"
                Return
            End If
    
    
            Response.ContentType = ContentType
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(HdAttachmentFileName.Value))
            Response.WriteFile(HdAttachmentFileName.Value)
            Response.Flush() 
            Response.End()
        End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多