2017-04-26 78 views
1

我有一个ASP.NET的GridView它使用模型绑定时发送参数BLL:使用Web窗体模型绑定和文件上传在GridView

<asp:GridView runat="server" ID="teamMembersGrid" 
... 
ItemType="Models.TeamMember" 
OnCallingDataMethods="teamMembersGrid_CallingDataMethods"> 

我也有一个BLL定义为UpdateTeamMember,InsertTeamMember方法,等等。

网格的其中一列显示图像。在编辑模式下,我需要使用FileUpload控件上传此列的新图像。我想将此控件作为参数传递给BLL方法UpdateTeamMember。问题是:如何?

我在页面外添加了一个ID为“FileUpload”的FileUpload控件。它有style =“display:none”(我试图去除这种风格,但它没有帮助)。然后,在RowUpdating,我设定此控制的值:

protected void teamMembersGrid_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    FileUpload = teamMembersGrid.Rows[e.RowIndex].FindControl("FileUpload") as FileUpload; 
} 

在该方法中,FileUpload控件正确检索并且它具有所需的信息。

而在BLL,我有这样的:

public void UpdateTeamMember(int teamMemberID, ModelMethodContext context, [Control("FileUpload")] FileUpload fileUpload) 
{ 
... 
} 

在BLL的这种方法,文件上传参数不为空,但并没有任何文件上传。

任何帮助,非常感谢。谢谢!

回答

0

嗨我得到了解决方案使用相同的webforms modelbinding。 在您身在何处插入模式 在ASPX在FormView控件标签在这个片段中添加此这里是

addCustomer

OnItemInserting="addCustomer_ItemInserting" 

然后在aspx.cs代码隐藏

FormView控件
protected void addCustomer_ItemInserting(object sender, FormViewInsertEventArgs e) 
     { 

      FileUpload Fileupload = (FileUpload)addCustomer.FindControl("CustomerImage"); 

      byte[] covertedByteArr = // convert stream to bytes here ConvertStreamToBytes(Fileupload.PostedFile.InputStream); 
      e.Values["CustomerImage"] = covertedByteArr; 
     } 

CustomerImage是模型中字段的名称wh ich应该是byte []类型的。 然后在BL层的InsertMethod上,你会看到字节[]被正确填充