轉(zhuǎn)帖|其它|編輯:郝浩|2011-09-14 14:36:28.000|閱讀 1329 次
概述:在網(wǎng)頁中經(jīng)常要動態(tài)添加控件的方法,例如上傳多個文件,而具體不知道上傳多少個文件,通過添加附件按鈕來動態(tài)添加FileUPLoad控件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在網(wǎng)頁中經(jīng)常要動態(tài)添加控件的方法,例如上傳多個文件,而具體不知道上傳多少個文件,通過添加附件按鈕來動態(tài)添加FileUPLoad控件。具體做法:
第一種方法:在網(wǎng)頁中添加一個Panel1容器控件。在Panel1的容器控件中添加FileUpLoad控件。
添加一個命令按鈕Button1,Button1.Text="添加附件";
后臺代碼:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Blog_NewBlog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["nClick"] = 0;//定義ViewState.
}
}
private void LoadFileupLoad(int n)//創(chuàng)建自定義方法,用于添加FileUpLoad控件。
{
FileUpload imgfile = new FileUpload();
imgfile.ID = "FileUpload" + n.ToString().Trim();
imgfile.Width = 500;
this.Panel2.Controls.Add(imgfile);
}
protected void Button1_Click(object sender, EventArgs e)
//添加附件按鈕事件
{
if (ViewState["nClick"] != null)
{
for (int i = 0; i <=
Convert.ToInt32(ViewState["nClick"].ToString()); i++)
{
LoadFileupLoad(i);//調(diào)用自定義方法。
}
ViewState["nClick"] =
Convert.ToInt32(ViewState["nClick"].ToString()) + 1;//ViewState加1
}
}
//引用動態(tài)添加的FileUpload控件,把上傳的文件保存到服務(wù)器上,把文件信息存儲到數(shù)據(jù)庫中。
protected void Button8_Click(object sender, EventArgs e)
{
OA jkj = new OA();//自定義類
bool ok = false;//上傳的文件是否符合要求
string Ftype = "";//文件類型。
string[] kzm = new string[]
{ ".jpg", ".tif", ".pdf", ".doc", ".xls", ".txt", ".ppt",".rar" };//上傳的文件類型
bool cb = false;//存儲插入數(shù)據(jù)的成功標(biāo)記。
jkj.StroeProName = "UP_BlogInsert";//存儲過程名稱
jkj.TableName = "Blog";//保存記錄的表名稱。
string bid = Guid.NewGuid().ToString();//生成一個唯一標(biāo)識。
string gk = "";
if (RadioButton1.Checked == true)
{
gk = "公開";
}
else
{
gk = "私人";
}
string[,] sql = new string[,]
{
{"@xm",Profile.xm,"InPut"},
{"@Btitle",jkj.HtmlCode(TextBox1.Text),"InPut"},
{"@Bcontent",FreeTextBox1.Text,"InPut"},
{"@ClassID",DropDownList1.SelectedValue,"InPut"},
{"@Bid",bid,"InPut"},
{"@state",gk,"InPut"},
{"@sendto",TextBox2.Text,"InPut"}
};
if (jkj.ExeComm(sql, "1") == "0")
{
cb = true;//保存成功。
}
HttpFileCollection files = Request.Files;//獲取動態(tài)控件FileUpload的集合。
string fileName;//文件名
int fsize = 0;//文件的大小
for (int i = 0; i < files.Count; i++)
{
if (files[i].ContentLength <= 0)
continue;
Ftype = System.IO.Path.GetExtension(files[i].FileName);
fsize = files[i].ContentLength;
for (int x = 0; x < kzm.Length; x++)
{
if (Ftype.ToLower() == kzm[x])
ok = true;
}
if (ok == true)
{
string upfilename = DateTime.Now.ToString("yyyymmddhhmmss") + Ftype;
string webpath = Server.MapPath("~/blog/upfiles/");
files[i].SaveAs(webpath + upfilename);//保存上傳的文件到服務(wù)器
jkj.StroeProName = "UP_BlogFile";
jkj.TableName = "BlogFile";
string[,] bf = new string[,]
{
{"@bid",bid,"InPut"},
{"@fName",upfilename,"InPut"},
{"@ftype",Ftype,"InPut"},
{"@fsize",fsize.ToString(),"InPut"}
};
if (jkj.ExeComm(bf, "1") == "0" && cb == true)
{
jkj.Show("保存成功!");
}
}
else
{
jkj.Show("上傳的文件格式符合要求!");
}
}
}
}
第二種方法:使用JavaScript
頁面如下:
《HTML>
《HEAD>
<script>
function AddAttachments() {
document.getElementById('attach').innerText = "繼續(xù)添加附件";
tb = document.getElementById('attAchments');
newRow = tb.insertRow();
newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'> <input type=button value='刪除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
}
function delFile(index)
{
document.getElementById('attAchments').deleteRow(index);
tb.rows.length > 0?document.getElementById('attach').innerText = "繼續(xù)添加附件":document.getElementById('attach').innerText = "添加附件";
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div><table id="attAchments"></table></div><span><IMG src="icoAddFl.gif"> </span> <A id="attach" style="font-family:宋體;font-size:9pt;" title="如果您要發(fā)送多個附件,您只需多次點擊“繼續(xù)添加附件”即可, 要注意附件總量不能超過發(fā)送限制的大小。" onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</A>
<br><br><br><br><br><br>
<asp:Button id="btnSend" runat="server" Text=" 上傳 " onclick="btnSend_Click"></asp:Button>
</form>
《/body>《/HTML>
后臺代碼
protected void btnSend_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
int attCount = 0;
string filePath = "";
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].ContentLength > 0)
{
filePath = Request.Files[i].FileName;
sb.Append("Files" + attCount++ + ": " + filePath + "<br>");
Request.Files[0].SaveAs(Server.MapPath("./") +
filePath.Substring(filePath.LastIndexOf("\\") + 1));
}
}
sb.Insert(0, "you upload " + attCount + " files.<br>");
Response.Write(sb.ToString());
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載