【问题标题】:file upload in gridview在gridview中上传文件
【发布时间】:2013-11-12 18:23:46
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您可以在以下范围内使用它:

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

    希望这会有所帮助...

    【讨论】:

    • 但是当我的gridview在updatepanel中时它不起作用
    【解决方案2】:

    以下链接对您有帮助:

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

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

    希望这会有所帮助...

    【讨论】:

      【解决方案3】:

      SudhaGridView 有一篇很棒的帖子,其中包含文件上传的全部功能:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:
          <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>
          

          【讨论】:

            猜你喜欢
            • 2015-12-09
            • 2015-05-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-10
            • 1970-01-01
            • 2016-08-11
            相关资源
            最近更新 更多