web对话框返回xml

来源:互联网 发布:手机淘宝店铺首页链接 编辑:程序博客网 时间:2024/06/05 21:10

1.对话框.aspx
对话框显示的是一个select,两个按钮添加和取消,注意<base target="_self">,
他保证了按照预期关闭对话框
<html>
<head>
<base target="_self">
</head>
<script>
 function WinClose()
 {
  window.close();   
 }
</script>
<body MS_POSITIONING="FlowLayout">
  <form id="Form1" method="post" runat="server">
   <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%"

border="0">
    <TR>
     <TD height="200"><select id="lstItems" style="WIDTH:

300px; HEIGHT: 253px" multiple size="15" name="lstItems"
       runat="server"></select></TD>
    </TR>
    <TR>
     <TD height="25" align="right"><INPUT class="FormButton"

id="btnAdd" type="button" value="添加" style="WIDTH: 60px; HEIGHT: 25px"
       name="Button1" runat="server"><FONT

face="宋体">&nbsp;</FONT><INPUT class="FormButton" id="btnCancel" style="WIDTH: 60px; HEIGHT:

25px" onclick="WinClose()"
       type="button" value="取消"></TD>
    </TR>
   </TABLE>
  </form>
 </body><html>
2.对话框.cs
....
//绑定控件
private void BindList()
{
....
....
 SqlDataReader dr=SqlHelper.ExecuteReader(str_connstring,CommandType.Text,sqlSelect);
 stItems.DataSource=dr;
 lstItems.DataValueField="instr_code";
 lstItems.DataTextField="instr_name";   
 DataBind();
}
//生成xml
private void btnAdd_ServerClick(object sender, System.EventArgs e)
{
string optionStr="<?xml version=/"1.0/" encoding=/"utf-8/" ?> <InstrList>";
foreach(System.Web.UI.WebControls.ListItem item in this.lstItems.Items)
{
 if(item.Selected)
 {
  optionStr+="<Instr code=/""+item.Value+"/">"+item.Text+"</Instr>";
    
 }
}
 optionStr+="</InstrList>";
 CloseWindow(optionStr) ;
}
//关闭并返回,关键在于这个返回值
private void CloseWindow(string returnValue)
{
 string script = @"<script>";
 script+= " window.close();returnValue='"+returnValue+"'; ";
 script+= "</script>";
 Response.Write(script);
}
3.父窗体.aspx 
<html>
<head></head>
<script>
//解析xml
function addInstrument(evl)
{   
 var openWindow =
 window.showModalDialog("对话框.aspx", "Value",

"dialogHeight:320px;dialogWidth:310px;status:no;help:no;scroll:no;resizable:no;center:yes;");
 if(openWindow!=null)
 {
  var oDoc = new ActiveXObject("MSXML2.DOMDocument");
  oDoc.loadXML(openWindow);
  var items=oDoc.selectNodes("InstrList/Instr");   
  for (var item = items.nextNode(); item; item = items.nextNode())
  {      
   var oOption = document.createElement("OPTION");
   oOption.text=item.nodeTypedValue;
   oOption.value=item.getAttribute("code");     

   document.all.getElementById("InstrList").add(oOption);
  }     

 }
}
//双击移除
function DelInstr(evl)

 evt = (evt) ? evt : ((window.event) ? window.event : "");
 if (evt) {
     var elem = (evt.target) ? evt.target : evt.srcElement;  
  elem.remove(elem.selectedIndex); 
 }  
}
//载入
windon.onload=function()
{
 var btnAdd=document.all.getElementById("addInstr");
 var selList=document.all.getElementById("");
 btnAdd.attachEvent("onclick",addInstrument);
 selList.attachEvent("ondblclick",DelInstr);
}
</script>
<body>
<INPUT type="button" value="增加" id="addInstr" >
<SELECT id="InstrList" size="4" style="WIDTH: 100%; HEIGHT: 70px"></SELECT>
</body>
</html>

原创粉丝点击