-
弹出窗返回数据实体对象或对象集
在项目中经常遇到字典数据的查询选择输入,为避免手工输入造成错误,常做成公共页(假设为test.aspx)以便在多处使用.下面利用Newtonsoft.Json序列化实体对象,以返回数据实体或实体集.
一:封装一个PopUpWindow工具类.需要引用Newtonsoft.Json.dll.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Newtonsoft.Json;
using System.Web;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ****.Core.Common
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 弹出窗口工具类
/// </summary>
/// <remarks>
/// 实现了弹出窗口返回对象的封装方法
/// </remarks>
/// Title: PopUpWindow
public class PopUpWindow
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将对象返回给调用页面
/// </summary>
/// <param name="entity">需要返回的对象实例</param>
/// <param name="page">调用此方法的page本身</param>
public static void ReturnObject(Object entity, Page page)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//使用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))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String cstext1 = String.Format("{0} window.returnValue = {1};window.close();", output, "retObj");
cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二:在公共页某按钮事件里设置返回数据,例如:
protected void Button1_Click1(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//创建需要返回的实体对象
SampleEntity entity = new SampleEntity();
entity.ProjectNo = TextBox1.Text;
entity.ProjectName = TextBox2.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//将对象返回给调用页面
PopUpWindow.ReturnObject(entity, this);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////创建需要返回的实体对象列表
//SampleEntity entity1 = new SampleEntity();
//entity1.ProjectNo = TextBox1.Text;
//SampleEntity entity2 = new SampleEntity();
//entity2.ProjectNo = TextBox2.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//List<SampleEntity> list = new List<SampleEntity>();
//list.Add(entity1);
//list.Add(entity2);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////将对象返回给调用页面
//PopUpWindow.ReturnObject(list, this);
}
三:在调用页放置一客户端图片控件,例如:
<img src="../images/lookup.gif" onclick="OnQueryClick()" id="btnImage" alt="点击我!" tyle="cursor: hand" />
然后在客户端脚本中加入如下代码即可,建议以模式窗口打开.
//获得弹出窗口的返回对象
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript">![](https://www.cnblogs.com/Images/dot.gif)
function OnQueryClick()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.getElementById("ctl00$ContentPlaceHolder1$txtProjectNo").value = returnValue.ProjectNo;
document.getElementById("ctl00$ContentPlaceHolder1$txtProjectName").value = returnValue.ProjectName;
}
}
</script>
-
相关阅读:
sock编程
Exceptional c++
sort
实现UDP高效接收/响应
Iterator invalidation(迭代器失效)
php 判断一个点是否在一个多边形区域内
PHP 如何在txt里查找包含某个字符串的那一行?
php 实现栈与队列
微信支付 接口
文章添加,删除,修改,分页列表
-
原文地址:https://www.cnblogs.com/jiangshaofen/p/717612.html
Copyright © 2020-2023
润新知