网格视图中的文件上传器 [英] File Uploader in Grid View

查看:59
本文介绍了网格视图中的文件上传器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在软件中创建客户时,我被困在一个需要上传图片以供购买证明的地方.

例如.客户A拥有2个产品,那么在保存记录的同时,有2个表受到影响,第1个CustomerDetails&第二个CustomerProductDetails.

在这种情况下,我在GridView中有多个FileUploader(在每一行中).

我尝试遍历Grid并通过查找控件来保存数据.

hi to all,

I am stuck at one place where I need to upload image for purchase proof while creation of customer in the software.

Eg. Customer A owned 2 products, then while saving the record, 2 tables get affected, 1st CustomerDetails & 2nd CustomerProductDetails.

In this case I have multiple FileUploader in GridView(In each row).

I try to loop over the Grid and saving the data by finding controls.

For Each row As GridViewRow In GVProduct.Rows
  Dim Proff As FileUpload = DirectCast(row.FindControl("pop_upload"), FileUpload)
  ...
Next


每当它的FileUploader出现时,它就会显示HasFile = false和FileName =".
FileName,PostedFile.FileName ..全部为空..
当我为每个上传者选择文件时.

请指导.


Whenever it''s FileUploader it shows HasFile = false and FileName="".
FileName, PostedFile.FileName.. all are empty..
When I have select file for every uploader.

please guide..
thanking you in advance..

推荐答案

您可以在下面的方法中使用它:

You can use this within the as follows:

<asp:templatefield headertext="UploadImage" xmlns:asp="#unknown">
   <itemtemplate>
      <asp:image imageurl="~/images/1.jpg" runat="server" id="image" /> // shown only when not in edit mode
    </itemtemplate>
   <edititemtemplate>
       <asp:fileupload id="FileUpload1" runat="server" /> // shown only in edit mode
   </edititemtemplate>
  </asp:templatefield>



最后包括以下内容以进入编辑模式.



At last include a as follows to enter edit mode.

<asp:commandfield showeditbutton="true" showcancelbutton="true" xmlns:asp="#unknown"> </asp:commandfield>


然后添加两个事件,如下所示:


Then add two events as follows:

   protected void GridView1_RowEditing(object sender, GridViewUpdateEventArgs e)
    {         
gvwID.EditIndex=e.NewEditIndex;
        BindGrid();
    }         
protected void GridView1_RowCancelEdit(object sender, GridViewUpdateEventArgs e)
     {             
gvwID.EditIndex=-1;
            BindGrid();
                  } 



FileUpload控件将不会自动保存上载的文件.要保存文件,您需要使用FileUpload控件的SaveAs方法.在使用SaveAs方法之前,您需要获取要编辑的行的FileUpload控件的实例.要获取控件的实例,您可以连接到GridView的RowUpdating事件.以下代码将获取FileUpload控件的实例并保存上载的文件:



The FileUpload control, will not automatically save the uploaded file. To save the file you need to use the FileUpload control’s SaveAs method. Before you can use the SaveAs method you need to get the instance of the FileUpload control for the row you are editing. To get the instance of the control you can hook up to the GridView’s RowUpdating event. The following code will get the instance of the FileUpload control and save the uploaded file:

   protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         int RowID=Convert.ToInt32(gvwID.DataKeys[e.RowIndex].value);
         FileUpload fileUpload = GridView1.Rows[e.RowIndex].FindControl("FileUpload1") as   FileUpload;
        if(fileUpload.HasFile)
      {
         fileUpload.SaveAs(System.IO.Path.Combine(Server.MapPath("Images"), fileUpload.FileName));
           //update db using the name of the file corresponding to RowID       
} 
        gvwID.EditIndex=-1;
        BindGrid();      } 


希望这会有所帮助...


Hope this will help...


这篇关于网格视图中的文件上传器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆