效果如图:
实现代码:
前台:
<script language="javascript">
function Popup(target,winWidth,winHeight,maxLength)
{
var popupWin = null;
var winLeft; //弹出的窗口距离屏幕左边的像素
var winTop; //弹出的窗口距离屏幕顶部的像素
var str; //弹出窗口的窗体属性值
//如果已经存在此弹出窗口,就关闭它
if(popupWin != null)
{
popupWin.close();
}
if(maxLength == null)
{
maxLength = 0;
}
//是弹出的窗口水平居中于屏幕
winLeft = (screen.width - winWidth) / 2;
//是弹出的窗口垂直居中于屏幕
winTop = (screen.height - winHeight) / 2;
//指定弹出窗口的外观属性
str="width="+winWidth+",height="+winHeight+",top="+winTop+",left="+winLeft+",scrollbars=0,resizable=no";
try
{
var popupStr = "";
popupWin = window.open('','popup',str);
popupStr += "<html> /r/n";
popupStr += " <head> /r/n";
popupStr += " <title>弹出窗体</title> /r/n";
popupStr += " <script language='javascript'> /r/n";
popupStr += " var maxLength="+maxLength+"; /r/n";
popupStr += " function GoBack() /r/n";
popupStr += " { if( (maxLength != 0) & (document.all['txt'].value.length>maxLength) ) /r/n";
popupStr += " { alert('你输入的字符长度超出范围!'); /r/n";
popupStr += " return; } /r/n";
popupStr += " window.opener.document.all['"+target+"'].value=txt.value; /r/n";
popupStr += " window.close(); /r/n";
popupStr += " } /r/n";
popupStr += " </"+"script> /r/n"
popupStr += " </head> /r/n";
popupStr += " <body bottommargin=0 topmargin=0 leftmargin=0 rightmargin=0> /r/n";
popupStr += " <center><textarea id=txt οnkeypress='javascript:if(event.keyCode==27)self.close();' style='width:"+(winWidth)+";height:"+(winHeight-25) +"'>";
popupStr += " </textarea> /r/n";
popupStr += " <input type=button id=btnSubmit value='OK' οnclick='GoBack()' style='width:60px'> /r/n";
popupStr += " <input type=button value='Cancel' οnclick='window.close()' style='width:60px'> /r/n";
popupStr += " </center></body> /r/n";
popupStr += "</html> /r/n";
popupWin.document.write(popupStr);
popupWin.focus();
popupWin.txt.value = document.all[target].value;
}
catch(e)
{
// alert(e.message);
popupWin.close();
popupWin=null;
}
}
</script>
后台代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
btnEdit.Attributes.Add("onclick","Popup('txtTest','500','300','"+txtTest.MaxLength+"');");
}
}