我在页面动态生成了一个 上传控件 
   现在我想上传图片或flash
     但是现在获得不到页面上的控件(动态生成的控件在页面上不显示)
      所以无法上传
         现在不知道该怎么做
 请各位多多指教

解决方案 »

  1.   

    protected void Page_Load(object sender, EventArgs e)
    {
        Button btn = new Button();
        btn.ID = "btn_bank_submit";
        btn.Text = "网上支付";
        btn.Click += new EventHandler(btn_bank_submit_Click);
        this.Controls.Add(btn);
    }类似这样 。
      

  2.   

    上传控件用JS生成<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    <style type="text/css">
    <!--
    ul, li {
    list-style-type: none;
    padding: 0px;
    margin: 0px;
    }
    a {
    color: #000;
    text-decoration: none;
    font-size: 12px;
    font-weight: bold;
    line-height: 30px;
    margin-left: 10px;
    }
    -->
    </style>
    <script type="text/javascript" src="../js/jquery-1.4.js"></script>
    <script type="text/javascript">
    var i=1;
    function add(){
    document.getElementById("list").innerHTML+="<li class=\"li\"><input type=\"file\" name=\"imgFile"+i+"\" /><a href=\"javascript:add();\">+</a><a href=\"javascript:del();\">-</a></li>";
    i+=1;
    }
    function del(){
    $(".li:first").detach();
    }
    </script>
    </head>
    <body>
    <div id="upload">
    <form action="../ashx/upload.ashx" method="post"  enctype="multipart/form-data">
        <ul id="list">
          <li><input type="file" name="imgfile" /><a href="javascript:add();">+</a></li>    
        </ul>
        <div>
          <input type="submit" value="上传" onclick="javascript:document.getElementById('upload').style.display='none'; document.getElementById('loading').style.display='block';" />
        </div>
    </form>
    </div>
    <div id="loading" style="display:none;">文件上传中……</div>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.IO;
    using System.Globalization;
    using System.Collections;namespace Web.asp_bin.admin.ashx
    {
        /// <summary>
        /// $codebehindclassname$ 的摘要说明
        /// </summary>
        public class upload : IHttpHandler
        {
            //文件保存目录路径     
            private String savePath = "/upload/images/" + DateTime.Now.ToString("yyyy") + DateTime.Now.ToString("MM") + "/";
            //文件保存目录URL
            private String saveUrl = "/upload/images/" + DateTime.Now.ToString("yyyy") + DateTime.Now.ToString("MM") + "/";        //定义允许上传的文件扩展名
            private String fileTypes = new Alvin.BLL.Setting().AttachmentType; //从网站设置中读取可以上传的类型//System.Configuration.ConfigurationManager.AppSettings["fileTypes"];
            //最大文件大小
            private int maxSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["maxSize"]) * 1024;
            private HttpContext context;
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                this.context = context;
                string strID=context.Request.QueryString["ID"];//返回的控件ID
                string path = context.Request.QueryString["Path"];
                if (!string.IsNullOrEmpty(path))
                {
                    if(System.IO.Directory.Exists(context.Server.MapPath(path)))
                    {
                        savePath=path;
                        saveUrl=path;
                    }
                }
                //HttpPostedFile imgFile = context.Request.Files[0];
                string retPath = string.Empty;
                for (int i=0; i<context.Request.Files.Count;i++)
                {
                    HttpPostedFile imgFile = context.Request.Files[i];
                    if (imgFile == null)
                    {
                        context.Response.Write("请选择文件。");
                        return;
                    }                String dirPath = context.Server.MapPath(savePath);
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);//如果不存在该文件夹,则创建文件夹
                    }                String fileName = imgFile.FileName;
                    String fileExt = Path.GetExtension(fileName).ToLower();
                    ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));                if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
                    {
                        context.Response.Write("<span style=\"margin: 0px;padding: 0px;font-size: 12px;\">上传文件大小超过限制。<a href=../inc/upload.aspx?ID=" + strID + ">点击重新选择上传</a></span>");
                        return;
                    }                if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
                    {
                        context.Response.Write("<span style=\"margin: 0px;padding: 0px;font-size: 12px;\">上传文件扩展名是不允许的扩展名。<a href=../inc/upload.aspx?ID=" + strID + ">点击重新选择上传</a></span>");
                        return;
                    }                String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + i + fileExt;
                    String filePath = dirPath + newFileName;
                
                    imgFile.SaveAs(filePath);
                    String fileUrl = saveUrl + newFileName;
                    retPath += fileUrl + ",";
                }
                retPath = retPath.Substring(0, retPath.Length - 1);
                if (string.IsNullOrEmpty(strID))
                {
                    context.Response.Write("<span style=\"margin: 0px;padding: 0px;font-size: 12px;\">上传成功!<a href=" + context.Request.UrlReferrer.ToString() + ">我还要上传</a></sapn>");
                }
                else
                {
                    context.Response.Write("<script type=\"text/javascript\"> parent.document.getElementById(\"" + strID + "\").value=\"" + retPath + "\";</script><span style=\"margin: 0px;padding: 0px;font-size: 12px;\">上传成功!<a href=" + context.Request.UrlReferrer.ToString() + ">点击重新上传</a></sapn>");
                }
            }        public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }