在窗体间传递实体数据

来源:互联网 发布:js 字符串转换日期 编辑:程序博客网 时间:2024/06/05 19:57

一:写个工具类,先引用Newtonsoft.Json.dll,用其序列化对象.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Newtonsoft.Json;
using System.Web;

namespace *****.Core.Common
{
    
/// <summary>
    
/// 弹出窗口工具类
    
/// </summary>
    
/// <remarks>
    
/// 实现了弹出窗口返回对象的封装方法
    
/// </remarks>
    
/// Title:     PopUpWindow

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

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

        }

    }

}

二:二:在公共页某按钮事件里设置返回数据,例如:

 

protected void Button1_Click1(object sender, EventArgs e)
    
{
     
//创建需要返回的实体对象
        SampleEntity entity = new SampleEntity();
        entity.ProjectNo 
= TextBox1.Text;
        entity.ProjectName 
= TextBox2.Text;        

        
//将对象返回给调用页面
        PopUpWindow.ReturnObject(entity, this);

        
/**/////创建需要返回的实体对象列表
        //SampleEntity entity1 = new SampleEntity();
        
//entity1.ProjectNo = TextBox1.Text;
        
//SampleEntity entity2 = new SampleEntity();
        
//entity2.ProjectNo = TextBox2.Text;

        
//List<SampleEntity> list = new List<SampleEntity>();
        
//list.Add(entity1);
        
//list.Add(entity2);
        
        
/**/////将对象返回给调用页面
        //PopUpWindow.ReturnObject(list, this);
    }

三:在调用页放置一客户端图片控件,例如: 然后在客户端脚本中加入如下代码即可,建议以模式窗口打开.

<img src="../images/lookup.gif" onclick="OnQueryClick()" id="btnImage"  alt="点击我!" tyle="cursor: hand" />

 

//获得弹出窗口的返回对象
  
<script type="text/javascript">
    
function OnQueryClick()
    
{
        
var address = "test.aspx";
        
var parameter1 = "";
        
var OpenStyle = "dialogWidth:650px;dialogHeight:550px;dialogLeft:200px;dialogTop:150px;center:yes;help:false;resizable:false;status:false";
        
var returnValue = window.showModalDialog(address,parameter1,OpenStyle);
       
        
//分别获取返回对象的属性
        if(returnValue != null)
        
{           
            document.getElementById(
"ctl00$ContentPlaceHolder1$txtProjectNo").value = returnValue.ProjectNo;    
        document.getElementById(
"ctl00$ContentPlaceHolder1$txtProjectName").value = returnValue.ProjectName;                               
        }
        
     }

    
</script>
原创粉丝点击