asp.net点击增加多行文件上传 删除

前台页面:

<div style="display:none">
<asp:NumberTextBox runat="server" ID="aaas"></asp:NumberTextBox></div>
<h2 class="mt10">促品/宣传物料信息</h2>

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
 <table border="0" cellspacing="0" cellpadding="0"  class="info_tab1 mt10" width="100%">
      <tr>
        <%--<th  style="width:5%">序号</th>
        <th  width="12%">礼品名称</th>
        <th width="10%">生产量</th>
        <th width="10%">生产成本(元)</th>
        <th width="10%">生产厂家</th>
        <th width="10%">备注</th>
        <th width="10%">使用方法</th>
        <th width="10%">详细说明</th>
        <th width="19%">物料图片</th>
        <th >操作</th>--%>
        <th  >序号</th>
        <th  >礼品名称</th>
        <th >生产量</th>
        <th >生产成本(元)</th>
        <th >生产厂家</th>
        <th >备注</th>
        <th >使用方法</th>
        <th >详细说明</th>
        <th >物料图片</th>
        <th >操作</th>
      </tr>
</HeaderTemplate>
      <ItemTemplate>
      <tr >
      <td >
        <%#Container.ItemIndex + 1%>
        </td>
        <td ><span class="redf">
         <asp:HiddenField ID="materialResourceID" runat="server" Value='<%#DataBinder.eval_r(Container.DataItem, "resource_id")%>' />
        <asp:TextBox ID="giftname"  size="13" MaxLength="40"  runat="server" Text='<%#DataBinder.eval_r(Container.DataItem, "present_name")%>'></asp:TextBox>
        <span class="title_red">*</span>
         </span>
         </td>
        <td >
       <asp:NumberTextBox ID="producenum" size="13" MaxLength="10"  runat="server" Model="Decimal" NumberOfDecimals="2" Text='<%#DataBinder.eval_r(Container.DataItem, "produce_count")%>'></asp:NumberTextBox>
        </td>
        <td ><span class="redf">
         <asp:NumberTextBox ID="producecost" size="13" MaxLength="10"  runat="server" Model="Decimal" NumberOfDecimals="2" Text='<%#DataBinder.eval_r(Container.DataItem, "produce_price")%>'></asp:NumberTextBox>
        </span></td>
        <td ><span class="redf">
          <asp:TextBox ID="producecompany" size="13" MaxLength="40"  runat="server" Text='<%#DataBinder.eval_r(Container.DataItem, "produce_vender")%>'></asp:TextBox>
        </span></td>
        <td ><span class="redf">
          <asp:TextBox ID="giftremark" size="13" MaxLength="90"  runat="server" Width="108px" Text='<%#DataBinder.eval_r(Container.DataItem, "remarks")%>'></asp:TextBox>
        </span></td>
        <td ><span class="redf">
          <asp:TextBox ID="usemethod" size="13" MaxLength="90"  runat="server" Width="117px" Text='<%#DataBinder.eval_r(Container.DataItem, "use_type")%>'></asp:TextBox>
        </span></td>
        <td >
        <span class="redf">
         <asp:TextBox ID="detailinfo" size="13" MaxLength="90"  runat="server" Width="105px" Text='<%#DataBinder.eval_r(Container.DataItem, "instraction")%>'></asp:TextBox>
        </span>
        </td>
         <td>
       
       <asp:HiddenField  ID="PicFilePathLink" runat="server"  Value='<%#DataBinder.eval_r(Container.DataItem, "PicFilePathLink")%>' />
       <asp:UpdatePanel ID="UpdatePanel4" runat="server" RenderMode="Block" UpdateMode="Conditional">
        <Triggers>
        <asp:PostBackTrigger ControlID="LinkButton3" />
              </Triggers>
       </asp:UpdatePanel>
     <asp:HyperLink ID="HyperLink2" runat="server" Target="_blank" Visible='<%#DataBinder.eval_r(Container.DataItem, "urlLinkVisiblity")%>' Text='<%#DataBinder.eval_r(Container.DataItem, "pic_filepath")%>'></asp:HyperLink>
     <asp:LinkButton Visible='<%#DataBinder.eval_r(Container.DataItem, "urlLinkVisiblity")%>' ID="LinkButton3" Text="[X]" runat="server" OnCommand="sell_delFile_Click" CommandName='<%#DataBinder.eval_r(Container.DataItem, "resource_id")%>'>
     </asp:LinkButton>
       <asp:HiddenField ID="HiddenField2" runat="server"    />
      <asp:UpdatePanel ID="UpdatePanel5" runat="server" Visible='<%#DataBinder.eval_r(Container.DataItem, "uploadVisiblity")%>' RenderMode="Block" UpdateMode="Conditional">
             <ContentTemplate>
                <asp:FileUpload ID="FileUpload2" runat="server" Width="250px"  onchange="PreviewImg_sell(this);"  /></ContentTemplate>
                     <Triggers>
                          <asp:PostBackTrigger ControlID="LinkButton4" />
                     </Triggers>
   </asp:UpdatePanel>
   <asp:LinkButton ID="LinkButton4" runat="server" style="display:none" Text="aa" OnClick="sell_MaterialUpload_Click"></asp:LinkButton> 
              
            </td>
           <td>
           <asp:LinkButton runat="server" ID="deleteBtn" OnCommand="deleteBtn_Click" CommandName='<%#DataBinder.eval_r(Container.DataItem, "resource_id")%>'   Text="删除"></asp:LinkButton>
         
           </td>
    </tr>
    </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
 
       
       
       
       
       
       
<div class="alignr pad10">选择产品数:<asp:DropDownList runat="server" ID="addProduceNumber">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="6" Value="6"></asp:ListItem>
<asp:ListItem Text="7" Value="7"></asp:ListItem>
<asp:ListItem Text="8" Value="8"></asp:ListItem>
<asp:ListItem Text="9" Value="9"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
</asp:DropDownList>
    <asp:Button Text="增 加" runat="server" ID="addProduceBtn"   OnClick="addMaterialBtn_Click" CssClass="btn_sorange" /></div>
 

<div class="alignc pb20 pt20">
 <asp:Button  ID="PromotionPlan" class="btn_sub"   Text="提交" runat="server"  OnClientClick='return check_input_null(CheckList);'
        onclick="SavePromotionPlan_Click"></asp:button>
     <asp:LinkButton runat="server" Text="保存草稿" ID="saveCBtn"   OnClientClick='return check_input_null(CheckList);'  OnClick="saveCBtn_Click"   ></asp:LinkButton>
 
</div>

 

 

后台处理:

 public DataTable GridViewDataTable
        {
            get { return ViewState["GridViewDataTable"] == null ? null : (DataTable)ViewState["GridViewDataTable"]; }
            set { ViewState["GridViewDataTable"] = value; }
        }

 public void fileUpload_material()
        {

            FileUpload FileUpload2 = new FileUpload();
            HiddenField HiddenField2 = new HiddenField();
            LinkButton LinkButton3 = new LinkButton();
            HyperLink HyperLink2 = new HyperLink();
            HiddenField PicFilePathLink = new HiddenField();

            foreach (RepeaterItem item in this.Repeater1.Items)
            {

                FileUpload2 = (FileUpload)item.FindControl("FileUpload2");
                HiddenField2 = (HiddenField)item.FindControl("HiddenField2");
                LinkButton3 = (LinkButton)item.FindControl("LinkButton3");
                HyperLink2 = (HyperLink)item.FindControl("HyperLink2");
                PicFilePathLink = (HiddenField)item.FindControl("PicFilePathLink");
                if (FileUpload2.PostedFile != null && FileUpload2.PostedFile.ContentLength > 0)
                {
                    string ext = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName).ToLower();
                    if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif" && ext != ".png")
                    {
                        this.Alert("请上传(*.jpg,*.jepg,*.bmp,*.gif,*.png)格式的图片!");
                        return;
                    }
                    string dir = Server.MapPath(PRODUCE_TEMP_FILE_PATH);
                    if (Directory.Exists(dir) == false)
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ext;
                    string path = PRODUCE_TEMP_FILE_PATH + filename;
                    FileUpload2.PostedFile.SaveAs(Server.MapPath(path));
                    HiddenField2.Value = path;
                    FileUpload2.Visible = false;
                    LinkButton3.Visible = true;
                    HyperLink2.Visible = true;
                    HyperLink2.Text = FileUpload2.FileName;
                    HyperLink2.NavigateUrl = path;
                    PicFilePathLink.Value = path;
                    break;

                }
                else
                {
                    //do some thing;
                   
                }

            }

          
        }


        protected void sell_MaterialUpload_Click(object sender, EventArgs e)
        {
            //FileUpload fu = (FileUpload)((LinkButton)sender).Parent.FindControl("FileUpload2");
          
            // string str = fu.PostedFile.FileName;
            fileUpload_material();
        }

        protected void sell_delFile_Click(object sender, CommandEventArgs e)
        {
            string resourceid = e.CommandName;
            DataTable dt = GridViewDataTable;
            refresh_Repeater(dt);
            foreach (DataRow dr in dt.Rows)
            {
                if (resourceid.Equals(dr["resource_id"]))
                {
                    string path = dr["PicFilePathLink"].ToString();
                    path = Server.MapPath(path);
                    FileInfo file = new FileInfo(path);

                    if (file.Exists)
                    {
                        file.Delete();
                    }
                    dr["pic_filepath"] = "";
                    //dr["PicFilePathLink"] = "";
                    dr["urlLinkVisiblity"] = false;
                    dr["uploadVisiblity"] = true;
                    break;
                }
            }
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
           }
      
       
        //---------------------------------------------------------------------------------------


        private DataTable getInitDataTable(DataTable dt)
        {
            if (dt == null)
            {
                dt = new DataTable();
                DataColumn dc;
                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "resource_id";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

 

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "present_name";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "produce_count";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "produce_price";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "produce_vender";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "remarks";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "use_type";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "instraction";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "pic_filepath";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

 

 

 

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.Boolean");
                dc.ColumnName = "uploadVisiblity";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);

                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.Boolean");
                dc.ColumnName = "urlLinkVisiblity";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);


                dc = new DataColumn();
                dc.DataType = System.Type.GetType("System.String");
                dc.ColumnName = "PicFilePathLink";
                dc.ReadOnly = false;
                dc.Unique = false;
                dt.Columns.Add(dc);
            }
            return dt;
        }

  protected void addMaterialBtn_Click(object sender, EventArgs e)
        {
            DataTable dt = GridViewDataTable;
          
            dt = getInitDataTable(dt);
                refresh_Repeater(dt);
                for (int i = 0; i < Convert.ToInt16(addProduceNumber.SelectedValue); i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["resource_id"] = System.Guid.NewGuid().ToString();
                    dr["present_name"] = "";
                    dr["produce_count"] = "0";
                    dr["produce_price"] = "0";
                    dr["produce_vender"] = "";
                    dr["remarks"] = "";
                    dr["use_type"] = "";
                    dr["pic_filepath"] = "";
                    dr["instraction"] = "";
                    dr["uploadVisiblity"] = true;
                    dr["PicFilePathLink"] = "";
                    dr["urlLinkVisiblity"] = false;
                    dt.Rows.Add(dr);
                }
                GridViewDataTable = dt;
                Repeater1.DataSource = GridViewDataTable;
                Repeater1.DataBind();


          
        }

        private void refresh_Repeater(DataTable dt)
        {
            foreach (RepeaterItem item in this.Repeater1.Items)
            {
                TextBox tN = (TextBox)item.FindControl("giftname");
                if (tN != null)
                {
                    dt.Rows[item.ItemIndex]["present_name"] = tN.Text;
                }
                NumberTextBox pN = (NumberTextBox)item.FindControl("producenum");
                if (pN != null)
                {
                    dt.Rows[item.ItemIndex]["produce_count"] =pN.Text;
                }
                NumberTextBox std = (NumberTextBox)item.FindControl("producecost");
                if (std != null)
                {
                    dt.Rows[item.ItemIndex]["produce_price"] = std.Text;
                }
                TextBox mq = (TextBox)item.FindControl("producecompany");
                if (mq != null)
                {
                    dt.Rows[item.ItemIndex]["produce_vender"] = mq.Text;
                }
                TextBox co = (TextBox)item.FindControl("giftremark");
                if (co != null)
                {
                    dt.Rows[item.ItemIndex]["remarks"] = co.Text;
                }
                TextBox rp = (TextBox)item.FindControl("usemethod");
                if (rp != null)
                {
                    dt.Rows[item.ItemIndex]["use_type"] = rp.Text;
                }
                TextBox wp = (TextBox)item.FindControl("detailinfo");
                if (wp != null)
                {
                    dt.Rows[item.ItemIndex]["instraction"] = wp.Text;
                }


                HiddenField hf = (HiddenField)item.FindControl("materialResourceID");
                if (hf != null)
                {
                    dt.Rows[item.ItemIndex]["resource_id"] = hf.Value;
                }

                HiddenField picl = (HiddenField)item.FindControl("PicFilePathLink");
                if (picl != null)
                {
                    dt.Rows[item.ItemIndex]["PicFilePathLink"] = picl.Value;
                }

 


                 dt.Rows[item.ItemIndex]["uploadVisiblity"] = true;
                 HyperLink  HL2 = (HyperLink)item.FindControl("HyperLink2");
                if(HL2!=null){
                    dt.Rows[item.ItemIndex]["pic_filepath"] = HL2.Text;
                    if (!string.IsNullOrEmpty(HL2.Text))
                    {
                        dt.Rows[item.ItemIndex]["uploadVisiblity"] = false;
                        dt.Rows[item.ItemIndex]["urlLinkVisiblity"] = true;
                    }
                    else
                    {
                        dt.Rows[item.ItemIndex]["uploadVisiblity"] = true;
                        dt.Rows[item.ItemIndex]["urlLinkVisiblity"] = false;
                    }
                }

 


              
            }
        }
      
        protected void deleteBtn_Click(object sender, CommandEventArgs e)
        {
            string resourceid = e.CommandName;
            DataTable dt = GridViewDataTable;
            refresh_Repeater(dt);
            foreach (DataRow dr in dt.Rows)
            {
                if (resourceid.Equals(dr["resource_id"]))
                {
                    dr.Delete();
                    break;
                }
            }
         
            GridViewDataTable = dt;
            Repeater1.DataSource = GridViewDataTable;
            Repeater1.DataBind();
        }


        protected List<ProductSellMaterialEntity> getProduceMaterialList(string productSellPlanID)
        {
            List<ProductSellMaterialEntity> list = new List<ProductSellMaterialEntity>();
            foreach (RepeaterItem item in this.Repeater1.Items)
            {
                ProductSellMaterialEntity psm = new ProductSellMaterialEntity();

 

              
                TextBox tN = (TextBox)item.FindControl("giftname");
              
                if (tN != null)
                {
                    psm.PresentName = tN.Text;
                    if (string.IsNullOrEmpty(psm.PresentName))
                    {
                        return null;
                    }

                }
                NumberTextBox pN = (NumberTextBox)item.FindControl("producenum");
                if (pN != null)
                {
                    psm.ProduceCount =Convert.ToDecimal(pN.Text);
                }
                NumberTextBox std = (NumberTextBox)item.FindControl("producecost");
                if (std != null)
                {
                    psm.ProducePrice = Convert.ToDecimal(std.Text);
                }
                TextBox mq = (TextBox)item.FindControl("producecompany");
                if (mq != null)
                {
                    psm.ProduceVender= mq.Text;
                }
                TextBox co = (TextBox)item.FindControl("giftremark");
                if (co != null)
                {
                    psm.Remarks = co.Text;
                }
                TextBox rp = (TextBox)item.FindControl("usemethod");
                if (rp != null)
                {
                    if (!string.IsNullOrEmpty(rp.Text))
                    {
                      psm.UseType = rp.Text;
                    }
                }
                TextBox wp = (TextBox)item.FindControl("detailinfo");
                if (wp != null)
                {
                    if (!string.IsNullOrEmpty(wp.Text))
                    {
                        psm.Instraction = wp.Text;
                    }
                }
              

                HiddenField hf = (HiddenField)item.FindControl("materialResourceID");
                if (hf != null)
                {
                   psm.ResourceId = hf.Value;
                }
                HiddenField pf = (HiddenField)item.FindControl("PicFilePathLink");

                if (pf != null)
                {
                  


                    if (!string.IsNullOrEmpty(pf.Value))
                {

                
                    string path = Server.MapPath(pf.Value);
                    string filetype = pf.Value.Substring(pf.Value.LastIndexOf("."));
                    FileInfo file = new FileInfo(path);
                    if (file.Exists)
                    {
                        string dir = Server.MapPath(PRODUCE_FILE_PATH);
                        if (Directory.Exists(dir) == false)
                        {
                            Directory.CreateDirectory(dir);
                        }
                        string fname = System.Guid.NewGuid().ToString() + filetype;
                        string targetFile = dir + fname;
                        if (path.IndexOf("ImportFiles") >= 0)
                        {
                            file.CopyTo(targetFile);
                        }
                        psm.PicFilepath = PRODUCE_FILE_PATH + fname;
                    }
                    if (path.IndexOf("ImportFiles") >= 0)
                    {
                        FileInfo filetemp = new FileInfo(path);
                        filetemp.Delete();
                    }
                }

        }
                psm.SellPlanId =productSellPlanID;
                list.Add(psm);
            }
            return list;
        }

 

 

 


                    foreach (ProductSellMaterialEntity psm in list2)
                    {
                        psm.SaveNew(broker);
                    }

 

 

 

 

 

 

 

你可能感兴趣的:(it)