弹出窗返回数据实体对象或对象集

      在项目中经常遇到字典数据的查询选择输入,为避免手工输入造成错误,常做成公共页(假设为test.aspx)以便在多处使用.下面利用Newtonsoft.Json序列化实体对象,以返回数据实体或实体集.
一:封装一个PopUpWindow工具类.需要引用Newtonsoft.Json.dll.
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Web.UI;
None.gif
using  Newtonsoft.Json;
None.gif
using  System.Web;
None.gif
None.gif
namespace   **** .Core.Common
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 弹出窗口工具类
InBlock.gif    
/// </summary>
InBlock.gif    
/// <remarks>
InBlock.gif    
/// 实现了弹出窗口返回对象的封装方法
InBlock.gif    
/// </remarks>
ExpandedSubBlockEnd.gif    
/// Title:     PopUpWindow    

InBlock.gif    public class PopUpWindow
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将对象返回给调用页面
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="entity">需要返回的对象实例</param>
ExpandedSubBlockEnd.gif        
/// <param name="page">调用此方法的page本身</param>        

InBlock.gif        public static void ReturnObject(Object entity, Page page)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//使用JSON序列化对象
InBlock.gif
            string output = String.Format("var retObj = {0};", JavaScriptConvert.SerializeObject(entity));
InBlock.gif            
InBlock.gif            
//注册脚本到弹出窗口以返回序列化后的对象
InBlock.gif
            Type cstype = HttpContext.Current.GetType();
InBlock.gif            ClientScriptManager cs 
= page.ClientScript;
InBlock.gif            
InBlock.gif            
//将实体返回给调用方caller
InBlock.gif
            String csname1 = "CloseScript";
InBlock.gif            
if (!cs.IsStartupScriptRegistered(cstype, csname1))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                String cstext1 
= String.Format("{0} window.returnValue = {1};window.close();", output, "retObj");
InBlock.gif                cs.RegisterStartupScript(cstype, csname1, cstext1, 
true);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
二:在公共页某按钮事件里设置返回数据,例如:
None.gif protected   void  Button1_Click1( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif     
//创建需要返回的实体对象
InBlock.gif
        SampleEntity entity = new SampleEntity();
InBlock.gif        entity.ProjectNo 
= TextBox1.Text;
InBlock.gif        entity.ProjectName 
= TextBox2.Text;        
InBlock.gif
InBlock.gif        
//将对象返回给调用页面
InBlock.gif
        PopUpWindow.ReturnObject(entity, this);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/////创建需要返回的实体对象列表
InBlock.gif        //SampleEntity entity1 = new SampleEntity();
InBlock.gif        
//entity1.ProjectNo = TextBox1.Text;
InBlock.gif        
//SampleEntity entity2 = new SampleEntity();
InBlock.gif        
//entity2.ProjectNo = TextBox2.Text;
InBlock.gif
InBlock.gif        
//List<SampleEntity> list = new List<SampleEntity>();
InBlock.gif        
//list.Add(entity1);
InBlock.gif        
//list.Add(entity2);
InBlock.gif
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**/////将对象返回给调用页面
InBlock.gif        //PopUpWindow.ReturnObject(list, this);
ExpandedBlockEnd.gif
    }
三:在调用页放置一客户端图片控件,例如:
None.gif   < img  src ="../images/lookup.gif"  onclick ="OnQueryClick()"  id ="btnImage"   alt ="点击我!"  tyle ="cursor: hand"   />
然后在客户端脚本中加入如下代码即可,建议以模式窗口打开.
None.gif //获得弹出窗口的返回对象
ExpandedBlockStart.gifContractedBlock.gif  
< script  type ="text/javascript" > dot.gif
InBlock.gif    
function OnQueryClick()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var address = "test.aspx";
InBlock.gif        
var parameter1 = "";
InBlock.gif        
var OpenStyle = "dialogWidth:650px;dialogHeight:550px;dialogLeft:200px;dialogTop:150px;center:yes;help:false;resizable:false;status:false";
InBlock.gif        
var returnValue = window.showModalDialog(address,parameter1,OpenStyle);
InBlock.gif       
InBlock.gif        
//分别获取返回对象的属性
InBlock.gif
        if(returnValue != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{           
InBlock.gif            document.getElementById(
"ctl00$ContentPlaceHolder1$txtProjectNo").value = returnValue.ProjectNo;    
InBlock.gif        document.getElementById(
"ctl00$ContentPlaceHolder1$txtProjectName").value = returnValue.ProjectName;                               
ExpandedSubBlockEnd.gif        }
        
ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif    
</ script >


转载于:https://www.cnblogs.com/jiangshaofen/archive/2007/04/18/717612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值