在gridview中上传文件

时间:2011-05-12 08:32:22

标签: c# asp.net gridview file-upload

我需要将一个带有文件上传控件的列添加到我的网格视图中,以便我可以针对任何特定行上传文件。是否可以这样做,理想情况下我需要能够在不将gridview置于其编辑状态的情况下执行此操作。

5 个答案:

答案 0 :(得分:10)

您可以在以下内容中使用此内容:

   <asp:TemplateField HeaderText="UploadImage">

   <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>

最后包括如下进入编辑模式。

       <asp:commandField showEditButton="true" showCancelButton="true"> 

然后按如下方式添加两个事件:

   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控件的实例并保存上载的文件:

   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();

    }

希望这会有所帮助......

答案 1 :(得分:0)

以下链接可以帮助您:

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

它有一个示例代码,用于将DateTimePicker添加到datagridview单元格。 您可以以相同的方式添加fileupload控件...

希望这会有所帮助......

答案 2 :(得分:0)

Sudha GridView中发布了一篇包含完整文件上传功能的帖子:

Fileupload control in gridview?


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="AccessDataSource1" Width="148px" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFileUpLoad" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
....

答案 3 :(得分:0)

EditTemplate看起来像这样:

    <EditItemTemplate>

       <asp:TextBox ID="txtImage" runat="server" Text='<%# Bind("Image") %>' Visible="False"></asp:TextBox>
       <asp:FileUpload ID="FileUpload1" runat="server" />

    </EditItemTemplate>

在后面的代码中,这将在行更新上传文件:

  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
    FileUpload FileUpload1 = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");

    if (FileUpload1 != null && FileUpload1.HasFile)
    {
        FileUpload1.SaveAs(Server.MapPath("~/uploads/" + myfilename));
    }
}

如果未选择任何文件,则会进行此检查,以便选择以前的名称。请注意,在编辑模板中,我们放置了一个文本框,其可见性设置为false,该文本框绑定到数据库中的图像名称

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (GridView1.EditIndex == -1) return;
    FileUpload fileUpLoad = GridView1.Rows[GridView1.EditIndex].FindControl("FileUpload1") as FileUpload;
    string fileName = fileUpLoad.FileName;
    TextBox txtImage = GridView1.Rows[GridView1.EditIndex].FindControl("txtImage") as TextBox;

    if (fileUpLoad != null && fileUpLoad.HasFile)
    {
        txtImage.Text = fileUpLoad.FileName;
    }
    else
    {
        txtImage.Text = txtImage.Text;
    }

}

答案 4 :(得分:0)

<asp:ScriptManager runat="server" ID="scm"></asp:ScriptManager>

<asp:UpdatePanel runat="server" ID="upMain" UpdateMode="Conditional"> 
<ContentTemplate>

  <asp:GridView AutoGenerateColumns="False" runat="server" ID="dt">  
<Columns>    
<asp:TemplateField HeaderText="Catagory">        
<ItemTemplate>            
<asp:DropDownList runat="server" ID="ddlSubCat">            
</asp:DropDownList>        
</ItemTemplate>    
</asp:TemplateField>    

<asp:TemplateField HeaderText="Attachments">        
<ItemTemplate>           

<asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="updFU">                   <ContentTemplate>      
<asp:FileUpload runat="server" ID="updCon" /><asp:Button runat="server" ID="btnUpload" Text="Upload" />            
</ContentTemplate>            
<Triggers>              
<asp:PostBackTrigger ControlID="btnUpload" />            
</Triggers>      
</asp:UpdatePanel>   

</ItemTemplate>    
</asp:TemplateField>  
</Columns>  
</asp:GridView>

</ContentTemplate>
</asp:UpdatePanel>