初学自定义控件,发现运行比较慢,求助!!
100c 2004-12-17 07:38:32 是一个上传文件的自定义控件
发现比正常的慢许多
求教各位高手
源程序如下:
<%@ Control Language="C#" ClassName="MyPostFile" %>
<script runat="server">
public string UpFileId;
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["hifCount"] = 1;
}
else
{
for (int intCounter = 2; intCounter <= (int)ViewState["hifCount"]; intCounter++)
{
AddNewhif(intCounter.ToString());
}
}
}
void add_Click(object sender, EventArgs e)
{
ViewState["hifCount"] = (int)ViewState["hifCount"] + 1;
AddNewhif(ViewState["hifCount"].ToString());
}
void AddNewhif(string strhigNum)
{
Literal NewLiteral = new Literal();
NewLiteral.Text = "<br>";
plh1.Controls.Add(NewLiteral);
System.Web.UI.HtmlControls.HtmlInputFile hifNew = new HtmlInputFile();
hifNew.ID = "file" + strhigNum;
plh1.Controls.Add(hifNew);
}
void btnOk_Click(object sender, EventArgs e)
{
//得到File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile;
string fileName,filetype,NewfileName;
int intCount;
for (intCount = 0; intCount < files.Count; intCount++)
{
postedFile = files[intCount];
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName == "")
{
break;
}
}
for (int j = 0; j < intCount; j++)
{
postedFile = files[j];
//获得文件名字
fileName = System.IO.Path.GetFileName(postedFile.FileName);
filetype = System.IO.Path.GetExtension(fileName).ToLower();
UpFileId = DateTime.Now.ToString("s").Replace("-", "").Replace(":", "") + intCount.ToString();
NewfileName = UpFileId + j.ToString() + filetype;
if (fileName != "")
{
//可根据不同的扩展名字,保存文件到不同的文件夹
//注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("Uploads/") + NewfileName);
}
}
}
</script>
<asp:PlaceHolder ID="plh1" Runat="Server" /><br />
<input type="file" id="file1" runat="server" name="myfile1" />
<asp:Button ID="add" Runat="server" Text="添加" OnClick="add_Click" /><br />
<asp:Button ID="btnOk" Runat="Server" Text="确定" OnClick="btnOk_Click" />