javascript动态生成的控件为何在刷新后就消失了 javascript动态生成的控件为何在刷新后就消失了请高手解答谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 javascript只是操纵客户端,刷新后,等价于从服务端重新下载页面,帮会消失。 无论是javascript或者服务器端动态加载的控件,刷新后,都会消失。 javascript是客户端脚本,写在客户端的,页面也是从服务器下载到本地运行,一刷新,然后又从服务器读取,下载到本地,所以出现这样的情况 //增加控件function addClientControls(){ var count_obj; var tr_obj; var td_obj; var file_obj; var form_obj; var count; var table_obj; var button_obj; var countview_obj; var str1; var str2; form_obj=document.getElementById("Form1"); fj_obj=document.getElementById("td_fj"); if(fj_obj.innertext=="无附件") { fj_obj.innertext=""; } count_obj=document.getElementById("count_obj"); if (count_obj==null) { count_obj=document.createElement("input"); count_obj.type="hidden"; count_obj.id="count_obj"; count_obj.value=1; form_obj.appendChild(count_obj); count=1; count_obj.value=1; } else { count_obj=document.getElementById("count_obj"); count=Number(count_obj.value)+1; count_obj.value=count; } div_obj=document.createElement("div"); div_obj.id="div_" + count; div_obj.align="center"; fj_obj.appendChild(div_obj); str1="附件名称:<input type='file' name='fj"+count+"' size=55 class='input' id=fj"+count+">"; str2="<br>附件标题:<input type='text' name='fjsm"+count+"' class='input' size=55 maxlength=100 id='fjsm"+count+"'>"; str3="<input type='button' class='button' value='删除本条' onclick=javascript:delClientControls("+"\""+div_obj.id+"\""+"); id='button"+count+"' name='button"+count+"'>"; div_obj.innerHTML=str1+str2+str3;}//删除控件function delClientControls(id) { var child; var parent; var count_obj; child=document.getElementById(id); count_obj=document.getElementById("count_obj"); if(child==null) { alert("对象为空"); } else { child.removeNode(true) count_obj.value=Number(count_obj.value)-1; if(count_obj.value=="0") { count_obj.removeNode(true); } } parent=document.getElementById("td_fj"); if(!parent.hasChildNodes) { parent.innerText=""; }} 后台获取用 Request获取表单信息。#region 上传新附件 ArrayList al =new ArrayList(); for(int i = 0;i<Request.Form.Count;i++) { if(Request.Form.Keys[i].ToString().Length>4) { if(Request.Form.Keys[i].ToString().Substring(0,4)=="fjsm") { al.Add(Request.Form[i].ToString()); } } } ///'遍历File表单元素 HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息 System.Text.StringBuilder strMsg = new System.Text.StringBuilder(); string fileName,fileSearchName, fileExtension,strFJXH1,strFJBT1,strDirectory,strSearchDirectory; try { for(int iFile = 0; iFile < files.Count; iFile++) { ///'检查文件扩展名字 HttpPostedFile postedFile = files[iFile]; fileName = System.IO.Path.GetFileName(postedFile.FileName); fileSearchName=fileName; fileExtension = System.IO.Path.GetExtension(fileName); strFJXH1 =(new AppDataCMP.DocumentManageCMP()).GetNewID("OA_DA_FJ","FJXH").ToString(); fileName="gxb_"+SystemManage.SystemUserManage.GetUserID(Session).ToString()+"_"+strFJXH1+fileExtension; fileSearchName=fileSearchName.Replace(fileExtension,"")+"["+strFJXH1+"]"+fileExtension; //生成目录 strDirectory=(new AppDataCMP.DocumentManageCMP()).GetDocumentAtthfilesUpLoadDirectory(System.Web.HttpContext.Current.Request.MapPath("DocumentAtthfiles/")); strSearchDirectory=(new AppDataCMP.DocumentManageCMP()).GetDocumentSearchUpLoadDirectory(System.Configuration.ConfigurationSettings.AppSettings["FilePath"],this.txtFBFL.Text+@"\"); //寻找附件标题 strFJBT1=al[iFile].ToString(); //增加到数据库 SortedList temp = new SortedList(); temp.Add("FJXH",strFJXH1); temp.Add("DAXH",this.txtDAXH.Text); temp.Add("FJBT",strFJBT1); temp.Add("CDLJ","documentatthfiles"+@"/"+strDirectory.Substring(strDirectory.Length-8,8)+@"/"+fileName); //temp.Add("CCGS",null) //temp.Add("XJCX",null); AppDataCMP.DocumentManageCMP dm = new AppDataCMP.DocumentManageCMP(); dm.Open(); dm.AddNewDocumentAtthfiles(temp); dm.Close(); //上传附件 postedFile.SaveAs(strDirectory +@"\"+ fileName); postedFile.SaveAs(strSearchDirectory+@"\"+ fileSearchName); } } catch(System.Exception Ex) { strStatus.Text = Ex.Message; } #endregion 重新从服务上下载了网页文件.你生成的只是客户端而已.了解下web原理先. 动软.NET代码生成器连接数据库时出错 分页排序显示页码算法? asp.net下使用多线程多么? 错在哪里 如何将vs2003web项目转换为vs2005 怎么在搜索关键字后 高亮显示 关键字 如何替换sql语句中的“'”单引号,防止程序出错。 Web.HttpContext.Current.Request.MapPath和....Server.MapPath 什么区别? datagrid中设置分页后导航按钮不能用也看不到下页的数据? 如何把一个DataGrid1的内容传给CrystalReport生成报表? response 关于图片上传
页面也是从服务器下载到本地运行,
一刷新,然后又从服务器读取,下载到本地,
所以出现这样的情况
function addClientControls()
{
var count_obj;
var tr_obj;
var td_obj;
var file_obj;
var form_obj;
var count;
var table_obj;
var button_obj;
var countview_obj;
var str1;
var str2;
form_obj=document.getElementById("Form1");
fj_obj=document.getElementById("td_fj");
if(fj_obj.innertext=="无附件")
{
fj_obj.innertext="";
}
count_obj=document.getElementById("count_obj"); if (count_obj==null)
{
count_obj=document.createElement("input");
count_obj.type="hidden";
count_obj.id="count_obj";
count_obj.value=1;
form_obj.appendChild(count_obj);
count=1;
count_obj.value=1;
}
else
{
count_obj=document.getElementById("count_obj");
count=Number(count_obj.value)+1;
count_obj.value=count;
}
div_obj=document.createElement("div");
div_obj.id="div_" + count;
div_obj.align="center";
fj_obj.appendChild(div_obj);
str1="附件名称:<input type='file' name='fj"+count+"' size=55 class='input' id=fj"+count+">";
str2="<br>附件标题:<input type='text' name='fjsm"+count+"' class='input' size=55 maxlength=100 id='fjsm"+count+"'>";
str3="<input type='button' class='button' value='删除本条' onclick=javascript:delClientControls("+"\""+div_obj.id+"\""+"); id='button"+count+"' name='button"+count+"'>";
div_obj.innerHTML=str1+str2+str3;
}//删除控件
function delClientControls(id)
{
var child;
var parent;
var count_obj; child=document.getElementById(id);
count_obj=document.getElementById("count_obj");
if(child==null)
{
alert("对象为空");
}
else
{
child.removeNode(true)
count_obj.value=Number(count_obj.value)-1;
if(count_obj.value=="0")
{
count_obj.removeNode(true);
}
} parent=document.getElementById("td_fj");
if(!parent.hasChildNodes)
{
parent.innerText="";
}
}
#region 上传新附件
ArrayList al =new ArrayList();
for(int i = 0;i<Request.Form.Count;i++)
{
if(Request.Form.Keys[i].ToString().Length>4)
{
if(Request.Form.Keys[i].ToString().Substring(0,4)=="fjsm")
{
al.Add(Request.Form[i].ToString());
}
}
} ///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
string fileName,fileSearchName, fileExtension,strFJXH1,strFJBT1,strDirectory,strSearchDirectory;
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
fileName = System.IO.Path.GetFileName(postedFile.FileName);
fileSearchName=fileName; fileExtension = System.IO.Path.GetExtension(fileName);
strFJXH1 =(new AppDataCMP.DocumentManageCMP()).GetNewID("OA_DA_FJ","FJXH").ToString();
fileName="gxb_"+SystemManage.SystemUserManage.GetUserID(Session).ToString()+"_"+strFJXH1+fileExtension;
fileSearchName=fileSearchName.Replace(fileExtension,"")+"["+strFJXH1+"]"+fileExtension;
//生成目录
strDirectory=(new AppDataCMP.DocumentManageCMP()).GetDocumentAtthfilesUpLoadDirectory(System.Web.HttpContext.Current.Request.MapPath("DocumentAtthfiles/"));
strSearchDirectory=(new AppDataCMP.DocumentManageCMP()).GetDocumentSearchUpLoadDirectory(System.Configuration.ConfigurationSettings.AppSettings["FilePath"],this.txtFBFL.Text+@"\");
//寻找附件标题
strFJBT1=al[iFile].ToString();
//增加到数据库
SortedList temp = new SortedList(); temp.Add("FJXH",strFJXH1);
temp.Add("DAXH",this.txtDAXH.Text);
temp.Add("FJBT",strFJBT1);
temp.Add("CDLJ","documentatthfiles"+@"/"+strDirectory.Substring(strDirectory.Length-8,8)+@"/"+fileName);
//temp.Add("CCGS",null)
//temp.Add("XJCX",null);
AppDataCMP.DocumentManageCMP dm = new AppDataCMP.DocumentManageCMP();
dm.Open();
dm.AddNewDocumentAtthfiles(temp);
dm.Close();
//上传附件
postedFile.SaveAs(strDirectory +@"\"+ fileName);
postedFile.SaveAs(strSearchDirectory+@"\"+ fileSearchName);
}
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
} #endregion
了解下web原理先.