我动态填加了几个<input type="file" id="files"....>
1)提交后动态填加的控件不见了
2)files.Count一直为0
下面是我的文档
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FileUpload.ascx.cs" Inherits="FileUpload" %>
<script type="text/javascript">
function AddAttachments()
{
document.getElementById('attach').innerText = "继续添加附件";
tb = document.getElementById('attAchments');
TR = tb.insertRow();
newTD1=TR.insertCell();
newTD2=TR.insertCell();
newTD1.innerHTML="<input type='file' id='upload' name='upload' size='50'>";
newTD2.innerHTML="<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 = "继续添加附件":document.getElementById('attach').innerText = "添加附件";
}
function checkFileSize(fileName)
{
var fso, s;
var maxsize = 3;
var file=document.getElementById(fileName).value;
if (file == "")
{
alert("路径不能为空!");
}
else
{
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.GetFile(file);
if (Math.round(file.size / 1024, 2) > maxsize * 1024)
{
alert("抱歉!您选择的文件为超过了程序" + maxsize + "MB的限制!");
return false;
}
}
}
</script>
<asp:ScriptManager ID="ScriptManger1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div>
<table id="attAchments">
<tr>
<td>
<input type="file" id="upload" name="upload" size="50" runat="server" />
</td>
<td>
<input type="button" value="删除" onclick="delFile(this.parentElement.parentElement.rowIndex)" />
</td>
</tr>
</table>
</div>
<div>
<a id="attach" style="font-family:宋体;font-size:9pt;" onclick="AddAttachments();" href="javascript:;" name="attach">添加附件</a>
<asp:Button id="btnSend" runat="server" Text=" 上传 " onclick="btnSend_Click"></asp:Button>
<br />
<asp:Label ID="construction" Font-Size="Small" Text="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小。" runat="server"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
同意,我以为是cs代码里Button btn1=new Button();
即使是这样,也要保存在Session里
js动态生成的控件是没有办法保存的,保存到ViewState里吧。
- - # 动态控件属于客户端创建的,又没有保存到ViewState里面去,提交之后服务器给你返回页面当然没了。