更新面板内部的Datalist链接按钮不是下载文件 [英] Datalist inside update panel linkbutton is not downloading file
本文介绍了更新面板内部的Datalist链接按钮不是下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataList内部更新面板linkbutton不下载文件。
以下是我的代码
Hi,
DataList Inside update panel linkbutton is not downloading file.
Below is my code
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="false">
<div style="vertical-align: middle; min-height: 480px;" class="pre-scrollable">
<div>
<asp:DataList ID="DataList2" runat="server" p Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table">
<ItemTemplate>
<div>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("ImagePath") %>' OnClick="DownloadFile"
CommandArgument='<%# Eval("Id")%>'></asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
</div>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Dim fileName as String="D:\Image\1.jpg"
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
'Response.ContentType = contentType
Response.AppendHeader("Content-Disposition", Convert.ToString("attachment; filename=") & fileName)
'Response.BinaryWrite(bytes)
Response.Flush()
Response.[End]()
我尝试过:
我添加了以下代码
What I have tried:
I added following code
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="LinkButton1" />
</Triggers>
但没有成功。
谢谢
Basit。
But no success.
Thanks
Basit.
推荐答案
我测试了你的代码并且它显示了一些错误。
错误1:两个具有相同名称,updatepanel和窗格组件的组件。
错误2:触发器显示错误(在UpdatePanel'UpdatePanel1'中找不到触发器ID为LinkButton1的控件。
解决方案
修复错误1 。
重命名其中一个。
修复错误2 。
删除aspx中的触发器组件和linkbutton的onclick事件。
在DataList中添加ItemCreated事件
Hi,
I tested your code and it shows me some errors.
Error 1: Two components with same name, updatepanel and pane component.
Error 2: The triggers show error (A control with the ID 'LinkButton1' was not found for the trigger in UpdatePanel 'UpdatePanel1').
SOLUTION
To fix error 1.
Rename one of them.
To fix error 2.
Remove the trigger component in your aspx and the onclick event of the linkbutton.
Add the ItemCreated event in the DataList
<asp:DataList ID="DataList2" runat="server" p Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table" OnItemCreated="DataList2_ItemCreated">
在代码隐藏中,将事件添加到DataList中创建的每一行。
C#
In the code-behind, add the event to each row created in your DataList.
C#
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Assigns the event to each radiobutton item
((LinkButton)e.Item.FindControl("LinkButton1")).Click += new System.EventHandler(DownloadFile);
}
VB.NET
VB.NET
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
'Assigns the event to each radiobutton item
Dim link As LinkButton = e.Item.FindControl("LinkButton1")
AddHandler link.Click, AddressOf DownloadFile
End If
当您点击链接按钮时,事件就会触发。
这里!完整代码
ASPX代码
When you click the linkbutton, the event fire.
Here! The Complete code
ASPX CODE
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head><%@ Page Language="vb" AutoEventWireup="false" CodeBehind="frm_my_test1.aspx.vb" Inherits="vb_web_app_teste.frm_my_test1" %>
<body>
<form id="form1" runat="server">
<asp:scriptmanager runat="server"></asp:scriptmanager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="Panel1" ChildrenAsTriggers="false">
<div style="vertical-align: middle; min-height: 480px;" class="pre-scrollable">
<div>
<asp:DataList ID="DataList2" runat="server" OnItemCreated="DataList2_ItemCreated" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table">
<ItemTemplate>
<div>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("ImagePath") %>'
CommandArgument='<%# Eval("Id")%>'></asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
</div>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
CODE-BEHIND C#
CODE-BEHIND C#
protected void DownloadFile(object sender, EventArgs e)
{
//your download code
string localFilenameDestiny = @"D:\Image\1.jpg";
using (WebClient client = new WebClient())
{
client.DownloadFile("http://your_domain_application/1.jpg", localFilenameDestiny);
}
}
protected void DataList2_ItemCreated(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//Assigns the event to each radiobutton item
((LinkButton)e.Item.FindControl("LinkButton1")).Click += new System.EventHandler(DownloadFile);
}
}
代码来自VB.NET
CODE-BEHIND VB.NET
Imports System.Net
Public Class frm_my_test1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.Add("Id")
dt.Columns.Add("FileName")
dt.Columns.Add("ImagePath")
Dim dr As DataRow = dt.NewRow()
dr("Id") = "1"
dr("FileName") = "A"
dr("ImagePath") = "Clique"
dt.Rows.Add(dr)
Me.DataList2.DataSource = dt
Me.DataList2.DataBind()
End Sub
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
Dim localFilename As String = "F:\1.png" 'path the destiny file
Dim client As New WebClient()
'localhost:50751. My local host. The image is in root the application
client.DownloadFile("http://localhost:50751/imginfo.png", localFilename)
End Sub
Protected Sub DataList2_ItemCreated(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
'Assigns the event to each radiobutton item
Dim link As LinkButton = e.Item.FindControl("LinkButton1")
AddHandler link.Click, AddressOf DownloadFile
End If
End Sub
End Class
The declarativePostBackTrigger
only works for a single control outside of a data-bound list. You’re using aDataList
, which means that there could be multiple copies ofLinkButton1
, so the declarative trigger will not work.
You’ll need to register the trigger from the code-behind instead:
The declarativePostBackTrigger
only works for a single control outside of a data-bound list. You're using aDataList
, which means that there could be multiple copies ofLinkButton1
, so the declarative trigger will not work.
You'll need to register the trigger from the code-behind instead:
Protected Sub DataList2_ItemCreated(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim btn As Control = e.Item.FindControl("LinkButton1")
If btn IsNot Nothing Then
ScriptManager.RegisterPostBackControl(btn)
End If
End If
End Sub
这篇关于更新面板内部的Datalist链接按钮不是下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文