zoukankan      html  css  js  c++  java
  • 利用自定义事件结合ModalPopup写一个通用的MsgBox类

    --ModalPopup.cs
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Web.UI.WebControls;
    using AjaxControlToolkit;

    namespace ajaxcs
    {
        public class ModalPopup
        {
            public delegate void OnMsgBoxEventHandler(string ReturnValue);
            public class MsgBox
            {
                    private Button CancelButton;
                    private HiddenField HiddenField1;
                    private ModalPopupExtender ModalPopupExtender1;
                    private Panel MsgBoxPanel;
                    private Label MsgLabel;
                    private Button OkButton;
                    private Label TitleLabel;

                    public event OnMsgBoxEventHandler OnMsgBox;

                    public MsgBox()
                    {

                        this.HiddenField1 = new HiddenField();
                        this.ModalPopupExtender1 = new ModalPopupExtender();
                    }

                    public void Initial(Panel MsgBoxPanel, Label MsgLabel, Label TitleLabel, Button OkButton, Button CancelButton)
                    {
                        this.ModalPopupExtender1.ID = MsgBoxPanel.ID + "_ModalPopupExtender1";
                        this.HiddenField1.ID = MsgBoxPanel.ID + "_HiddenField1";
                        MsgBoxPanel.Parent.Controls.Add(this.HiddenField1);
                        MsgBoxPanel.Parent.Controls.Add(this.ModalPopupExtender1);
                        this.ModalPopupExtender1.TargetControlID = this.HiddenField1.ClientID;
                        this.ModalPopupExtender1.PopupControlID = MsgBoxPanel.ID;
                        this.MsgBoxPanel = MsgBoxPanel;
                        this.MsgLabel = MsgLabel;
                        this.TitleLabel = TitleLabel;
                        this.OkButton = OkButton;
                        this.CancelButton = CancelButton;
                        if (MsgBoxPanel.Page.Request.Form["__EVENTTARGET"] == this.HiddenField1.ClientID)
                        {
                            if (OnMsgBox != null)
                                OnMsgBox(MsgBoxPanel.Page.Request.Form[this.HiddenField1.ClientID]);
                        }
                    }
     
                    public void Show(string msg, [Optional, DefaultParameterValue("询问窗口")] string title, [Optional, DefaultParameterValue("")] string ModalBackgroundStyle, [Optional, DefaultParameterValue(100)] int x, [Optional, DefaultParameterValue(100)] int y)
                    {
                        string js = "<script>";
                        js = (((js + "function _onMsgBoxOK(v){") + "$get('" + this.HiddenField1.ClientID + "').value=v;") + this.MsgBoxPanel.Page.ClientScript.GetPostBackEventReference(this.MsgBoxPanel.Page.FindControl(this.ModalPopupExtender1.TargetControlID), "")) + "}" + "</script>";
                        this.MsgBoxPanel.Page.ClientScript.RegisterStartupScript(typeof(string), "showModalWindow_MsgBox", js);
                        this.ModalPopupExtender1.TargetControlID = this.HiddenField1.ClientID;
                        this.ModalPopupExtender1.PopupControlID = this.MsgBoxPanel.ID;
                        this.ModalPopupExtender1.BackgroundCssClass = ModalBackgroundStyle;
                        this.ModalPopupExtender1.DropShadow = true;
                        this.ModalPopupExtender1.OkControlID = this.OkButton.ID;
                        this.ModalPopupExtender1.OnOkScript = "_onMsgBoxOK(true)";
                        this.ModalPopupExtender1.OnCancelScript = "_onMsgBoxOK(false)";
                        this.ModalPopupExtender1.CancelControlID = this.CancelButton.ID;
                        this.ModalPopupExtender1.PopupDragHandleControlID = this.TitleLabel.ID;
                        this.ModalPopupExtender1.X = x;
                        this.ModalPopupExtender1.Y = y;
                        this.ModalPopupExtender1.OkControlID = this.OkButton.ID;
                        this.MsgLabel.Text = msg;
                        this.TitleLabel.Text = title;
                        this.ModalPopupExtender1.Show();
                    }

                   
            }
        }
    }
    --调用后台代码

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using ajaxcs;

    namespace ajaxcs
    {
        public partial class _Default : System.Web.UI.Page
        {
            ModalPopup.MsgBox MsgBox1 = new ModalPopup.MsgBox();
            protected void Page_Load(object sender, EventArgs e)
            {
                MsgBox1.OnMsgBox += new ModalPopup.OnMsgBoxEventHandler(MsgBox1_OnMsgBox);
                MsgBox1.Initial(this.Panel1, this.Label2, this.LBL_Title, this.btn_OK, this.btn_Cancel);
               

            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                MsgBox1.Show(this.txb_body.Text, this.txb_title.Text, "modalbackground", 120, 100);
            }
            protected void MsgBox1_OnMsgBox(string ReturnValue)
            {
                Response.Write(ReturnValue);
            }
        }
    }

    --前台代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>未命名页面</title>
        <style type="text/css">
    .modalbackground
    {
        border: 1px solid #2F4F4F;
        color: white;
        background-color: #000000;
        font-weight: bold;
        cursor: not-allowed;
        filter: Alpha(opacity=40);
    }
    </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            ModalPopup - MsgBox(用户输入的值可返回值给后端)<br />
            <br />
            范例:<br />
            <br />
            标题:<asp:TextBox ID="txb_title" runat="server">询问:</asp:TextBox><br />
            内文:<asp:TextBox ID="txb_body" runat="server">您确定要这怎么做?</asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="ShowByPostback"
                onclick="Button1_Click" /><br />
            <br />
            <asp:Panel ID="Panel1" runat="server" BackColor="WhiteSmoke" BorderColor="#0000C0"
                BorderStyle="Solid" BorderWidth="1px" Height="168px" HorizontalAlign="Center"
                Style="display: none" Width="296px">
                <span style="color: #ffffff"></span>
                <asp:Label ID="LBL_Title" runat="server" BackColor="Blue" Font-Bold="True" ForeColor="White"
                    Style="text-align: left" Text="标题" Width="100%"></asp:Label><br />
                <br />
                <asp:Label ID="Label2" runat="server" ForeColor="Black" Height="72px" Text="Label"
                    Width="100%"></asp:Label><br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                <table style=" 100%">
                    <tr>
                        <td align="right">
                            <asp:Button ID="btn_OK" runat="server" Text="确定" />
                            <asp:Button ID="btn_Cancel" runat="server" Text="取消" /></td>
                    </tr>
                </table>
                &nbsp;&nbsp;
            </asp:Panel>
       
        </div>
        </form>
    </body>
    </html>

  • 相关阅读:
    5.19 省选模拟赛 T1 小B的棋盘 双指针 性质
    5.15 省选模拟赛 容斥 生成函数 dp
    5.15 省选模拟赛 T1 点分治 FFT
    5.15 牛客挑战赛40 B 小V的序列 关于随机均摊分析 二进制
    luogu P4929 【模板】舞蹈链 DLX
    CF 878E Numbers on the blackboard 并查集 离线 贪心
    5.10 省选模拟赛 拍卖 博弈 dp
    5.12 省选模拟赛 T2 贪心 dp 搜索 差分
    5.10 省选模拟赛 tree 树形dp 逆元
    luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树
  • 原文地址:https://www.cnblogs.com/hhq80/p/1512525.html
Copyright © 2011-2022 走看看