[英]Upload files in Gridview and display in the same row upon completion
I am trying to store files from a Gridview upload
into a Folder using Asp.net. 我试图使用Asp.net将文件从
Gridview upload
存储到文件夹中。
This is my markup code to generate Upload column and The button and File Upload control. 这是我的标记代码,用于生成“上载”列和“按钮”和“文件上载”控件。
<asp:TemplateField HeaderText="Upload">
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" AllowMultiple="true" />
<asp:Button ID="btnUpload" runat="server" CommandName="Upload" Text="OK" style=" color: #ff0000" OnClick="btnUpload_Click"/>
</ItemTemplate>
</asp:TemplateField>
I have my codebehind to handle btnUpload_Click as below: 我有我的代码隐藏来处理btnUpload_Click ,如下所示:
protected void btnUpload_Click(object sender, GridViewCommandEventArgs e)
{
Response.Write("File has been passed");
Button bts = e.CommandSource as Button;
Response.Write(bts.Parent.Parent.GetType().ToString());
if (e.CommandName.ToLower() != "upload")
{
return;
}
FileUpload fu = bts.FindControl("FileUpload4") as FileUpload;//here
if (fu.HasFile)
{
bool upload = true;
string fleUpload = Path.GetExtension(fu.FileName.ToString());
if (fleUpload.Trim().ToLower() == ".xls" | fleUpload.Trim().ToLower() == ".xlsx")
{
fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
//Someting to do?...
}
else
{
upload = false;
// Something to do?...
}
if (upload)
{
// somthing to do?...
}
}
else
{
//Something to do?...
}
}
I am getting this error: 我收到此错误:
CS0123: No overload for 'btnUpload_Click' matches delegate 'System.EventHandl
CS0123:'btnUpload_Click'没有重载匹配委托'System.EventHandl
Can somebody please help me? 有人能帮帮我吗?
You're binding both Command
and Click
event with your button. 您使用按钮绑定了
Command
和Click
事件。 Your button code should be like this- 你的按钮代码应该像这样 -
<asp:TemplateField HeaderText="Upload">
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" AllowMultiple="true" />
<asp:Button ID="btnUpload" runat="server" CommandName="Upload" Text="OK" style=" color: #ff0000"/>
</ItemTemplate>
</asp:TemplateField>
and from code behind capture the command not the event. 从代码后面捕获命令而不是事件。 like-
喜欢-
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Write("File has been passed");
Button bts = e.CommandSource as Button;
Response.Write(bts.Parent.Parent.GetType().ToString());
if (e.CommandName.ToLower() != "upload")
{
return;
}
FileUpload fu = bts.FindControl("FileUpload1") as FileUpload;//here
if (fu.HasFile)
{
bool upload = true;
string fileName = Path.GetFileName(fu.PostedFile.FileName);
string fleUpload = Path.GetExtension(fu.PostedFile.FileName);
if (fleUpload.Trim().ToLower() == ".xls" || fleUpload.Trim().ToLower() == ".xlsx")
{
fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fileName));
string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fileName ));
//Someting to do?...
}
else
{
upload = false;
// Something to do?...
}
if (upload)
{
// somthing to do?...
}
}
else
{
//Something to do?...
}
}
above I'm assuming your gridview ID
is GridView1
. 上面我假设你的gridview
ID
是GridView1
。 And another one your File Uploader control's ID
was mismatched with your code behind. 另一个你的File Uploader控件的
ID
与后面的代码不匹配。 This should work. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.