C# 带有 FileUpload 控件的 Gridview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/783597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:00:02  来源:igfitidea点击:

Gridview with FileUpload control

c#asp.netgridviewfile-upload

提问by Kolten

I have a gridview that shows an image as part of one of its columns. In Edit mode, I would like to let the user have the ability to upload a new image file, so I am using the FileUpload control in the edit portion of the template.

我有一个 gridview,它显示图像作为其中一列的一部分。在编辑模式下,我想让用户能够上传新的图像文件,所以我在模板的编辑部分使用了 FileUpload 控件。

I have an event to capture this i believe:

我有一个来捕捉这个我相信:

        protected void GridVew1_RowUpdated(object sender, GridViewUpdateEventArgs e)
    {          
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("images/hardware/" + FileUpload1.FileName));
        }
    }

I do not know how to call the control correctly though... How is this functionality coded?

我不知道如何正确调用控件...这个功能是如何编码的?

采纳答案by Jose Basilio

First you need to handle the RowUpdatingevent instead of RowUpdated. Then you need to find a reference to the FileUpload control on that row.

首先,您需要处理RowUpdating而不是RowUpdated。然后您需要在该行上找到对 FileUpload 控件的引用。

IMPORTANT:You need to know the ordinal position of the column where the control is located. In my example, I set it to 0, assuming it is the first column.Otherwise, you would need to through the Cells collection to find it.

重要提示:您需要知道控件所在列的顺序位置。在我的示例中,我将其设置为 0,假设它是第一列。否则,您需要通过 Cells 集合来找到它。

protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gridView.Rows[e.RowIndex];
    FileUpload fileUpload = row.Cells[0].FindControl("fileUpload1") as FileUpload;
    if (fileUpload != null && fileUpload.HasFile)
    {
        fileUpload.SaveAs(Server.MapPath("images/hardware/" + fileUpload.FileName));
    }
}

回答by braindice

If I understand what you are doing here you will have to find the control in the row

如果我明白你在这里做什么,你将不得不在行中找到控件

so in VB some thing like this

所以在 VB 中是这样的

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

      Dim aRow As GridViewRow = Me.GridView1.Rows(e.RowIndex)

    dim xFileUpload as fileupload = CType(aRow.FindControl("FileUpload1"), FileUpload)

    xFileUpload. save file etc etc etc 

End Sub 

Caveat - if this is wrong I would love to see a better way to do this!

警告 - 如果这是错误的,我很想看到更好的方来做到这一点!