• 利用自定义事件结合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>

  • 相关阅读:
    9个免费的桌面产品自动化测试工具
    How to wait for any one of the two element to appear on the screen?
    git仓库过大致使clone失败的解决方法
    Maven项目打包出现:No compiler is provided in this environment. Perhaps you are running on a JRE rather than JDK
    eclipse java maven testng The import org.testng cannot be resolved
    Codeforces Round #165 (Div. 1) B 269B Greenhouse Effect
    Codeforces Round #162 (Div. 1) B 264B Good Sequences
    HDU 4512 HDOJ 吉哥系列故事——完美队形I 腾讯2013初赛第三场
    HDU 4483 HDOJ Lattice triangle
    第二届腾讯校园编程马拉松参赛感想 极限!马拉松
  • 原文地址:https://www.cnblogs.com/hhq80/p/1512525.html
Copyright © 2020-2023  润新知