zoukankan      html  css  js  c++  java
  • 弹出窗返回数据实体对象或对象集

          在项目中经常遇到字典数据的查询选择输入,为避免手工输入造成错误,常做成公共页(假设为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;

    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>


  • 相关阅读:
    小笔记系列——Excel中获取当前日期
    Git 错误:OpenSSL SSL_read: Connection was reset, errno 10054
    cmd_切换文件目录的几种方法
    Jupyter Notebook 常用操作(持续更新中……)
    chrome 浏览器书签保存
    各种开发工具注释的快捷键(持续更新中…)
    Spyder 快捷键(注释、跳转、缩进)
    ISlide插件安装后,PPT无法正常关闭
    [TimLinux] 操作系统实战45讲
    [TimLinux] vnc and go bashrc
  • 原文地址:https://www.cnblogs.com/jiangshaofen/p/717612.html
Copyright © 2011-2022 走看看