asp.net中獲取Layer彈出層返回值

暖楓無敵發表於2015-12-23

1、MainPage.aspx中點選按鈕利用Layer彈出層,程式碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="demo.MainPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="../Scripts/Jquery-1.8.0.min.js"></script>
    <script src="../Scripts/layer/layer.js"></script>
    <script type="text/javascript">
        $(function () {
            layer.config({
                extend: ['skin/espresso/style.css'], //載入新皮膚
                skin: 'layer-ext-espresso' //一旦設定,所有彈層風格都採用此主題。
            });
        });

        //彈出框
        function popUp() {
            var UnitCode = "110000";
            layer.open({
                title: ['高階查詢'],
                type: 2,
                content: "SubPage.aspx?UnitCode=" + UnitCode,
                area: ['500px', '520px'],
                shade: 0.3,
                btn: ['確定', '關閉'],
                yes: function (index) {
                    var res = window["layui-layer-iframe" + index].saveFunc();
                    if (res != false) {
                        $("#hidSelectUnit").val(res.SelectUnit);
                        $("#hidCompareDate").val(res.CompareDate);
                        $("#hidCompareTips").val(res.CompareTips);
                        $("#form1").submit();
                    }
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" action="?FormSubmit=true">
        <input type="button" id="btnSelect" onclick='popUp();' style="margin-left: 10px; font-family: 微軟雅黑; font-size: 14px;" value="高階查詢" />
        <asp:HiddenField ID="hidSelectUnit" runat="server" />
        <asp:HiddenField ID="hidCompareDate" runat="server" />
        <asp:HiddenField ID="hidCompareTips" runat="server" />
        <div id="divMsg" runat="server">
        </div>
    </form>
</body>
</html>

protected void Page_Load(object sender, EventArgs e)
{
      if (!string.IsNullOrEmpty(Request.QueryString["FormSubmit"]))
      {
          this.divMsg.InnerHtml = "單位編碼:" + hidSelectUnit.Value + "</br>對比日期:" + hidCompareDate.Value.Split(',')[0] + "|" + hidCompareDate.Value.Split(',')[1] + "</br>對比提示:" + hidCompareTips.Value.Split(',')[0] + "|" + hidCompareTips.Value.Split(',')[1];
      }
 }


重點程式碼:

 layer.open({
                title: ['高階查詢'],
                type: 2,
                content: "SubPage.aspx?UnitCode=" + UnitCode,
                area: ['500px', '520px'],
                shade: 0.3,
                btn: ['確定', '關閉'],
                yes: function (index) {
                    var res = window["layui-layer-iframe" + index].saveFunc();
                    if (res != false) {
                        $("#hidSelectUnit").val(res.SelectUnit);
                        $("#hidCompareDate").val(res.CompareDate);
                        $("#hidCompareTips").val(res.CompareTips);
                        $("#form1").submit();
                    }
                }
            });


2、SubPage.aspx是彈出框內容區域,程式碼如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SubPage.aspx.cs" Inherits="demo.SubPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>彈出頁面</title>
    <script src="../Scripts/Jquery-1.8.0.min.js"></script>
    <script src="../Scripts/layer/layer.js"></script>
    <script type="text/javascript">
        $(function () {
            layer.config({
                extend: ['skin/espresso/style.css'], //載入新皮膚
                skin: 'layer-ext-espresso' //一旦設定,所有彈層風格都採用此主題。
            });
        });

        function saveFunc() {
            var selectedUnit = $("#unitcode").val();
            var compareDate = new Array();
            compareDate[0] = "2015-12-01";
            compareDate[1] = "2015-12-23";
            var compareTips = new Array();
            compareTips[0] = "合肥";
            compareTips[1] = "六安";
            var returnJson = {
                "SelectUnit": selectedUnit,
                "CompareDate": compareDate,
                "CompareTips": compareTips
            }
            return returnJson;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
      <div>
          <input id="unitcode" type="text" />
      </div>
    </form>
</body>
</html>

3、效果如下圖所示:

確定後接受彈出層輸入的資訊及其他資訊,如下圖所示:


===========================================================================

如果覺得對您有幫助,微信掃一掃支援一下:






相關文章