现在的位置: 首页 > 综合 > 正文

获取 confirm 点击事件【自定义控件】

2012年09月06日 ⁄ 综合 ⁄ 共 5175字 ⁄ 字号 评论关闭

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

/*------------------------------------------------------------------------------------
 * 文件名:MessageBox.cs

 * 文件功能描述:Excel的导入导出
 *
 * 创建标识:刘佳忻     20081128
 *
 * 修改标识:
 * 修改描述:
 *
* ----------------------------------------------------------------------------------*/

namespace I799NET
{
 
   [Serializable, DesignTimeVisible, ToolboxData("<{0}:MessageBox runat=\"server\"></{0}:MessageBox>")]
public class MessageBox : WebControl
{
    // Fields
    private const string cancel = "cancel";//"cancel";
    private string content;
    private string m_Session = "msgSession";
    private const string notPressed = "notPressed";
    private const string ok = "ok";//"ok";

    // Events
    public event Message GetMessageBoxResponse;

    // Methods
    public void Alert(string message)
    {
        string str = message.Replace("\n", @"\n");
        str = message.Replace("\"", "'");
        StringBuilder builder = new StringBuilder(50);
        builder.Append("<script language='javascript'>");
        builder.Append("alert( \"" + str + "\" );");
        builder.Append("</script>");
        this.content = builder.ToString();
    }

    public void Confirm(string message)
    {
        this.Confirm(message, null);
    }

    public void Confirm(string message, object data)
    {
        string str = message.Replace("\n", @"\n");
        str = message.Replace("\"", "'");
        StringBuilder builder = new StringBuilder(100);
        builder.AppendFormat("<input type='hidden' value='{0}' name='" + this.HiddenFieldName + "' />", "notPressed");
        builder.Append("<script language='javascript' type='text/javascript'>");
        builder.Append(" if(confirm( \"" + str + "\" ))");
        builder.Append(" { ");
        builder.Append("document.forms[0]." + this.HiddenFieldName + ".value='ok';document.forms[0].submit(); }");
        builder.Append(" else { ");
        builder.Append("document.forms[0]." + this.HiddenFieldName + ".value='cancel'; document.forms[0].submit(); }");
        builder.Append("</script>");
        this.content = builder.ToString();
        if (data != null)
        {
            this.Page.Session[this.m_Session + this.ClientID] = data;
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        object data = null;
        string str = this.Page.Request.Form[this.HiddenFieldName];
        if (str != null)
        {
            if (!(str == "ok"))
            {
                if (str == "cancel")
                {
                    this.Page.Request.Form[this.HiddenFieldName].Replace("cancel", "notPressed");
                    if (this.Page.Session[this.m_Session + this.ClientID] != null)
                    {
                        data = this.Page.Session[this.m_Session + this.ClientID];
                        this.Page.Session.Remove(this.m_Session + this.ClientID);
                    }
                    this.OnMessageBoxShow(new MessageBoxEventHandler(MessageBoxEventHandler.Button.Cancel, data));
                }
                else if (str == "notPressed")
                {
                }
            }
            else
            {
                this.Page.Request.Form[this.HiddenFieldName].Replace("ok", "notPressed");
                if (this.Page.Session[this.m_Session + this.ClientID] != null)
                {
                    data = this.Page.Session[this.m_Session + this.ClientID];
                    this.Page.Session.Remove(this.m_Session + this.ClientID);
                }
                this.OnMessageBoxShow(new MessageBoxEventHandler(MessageBoxEventHandler.Button.Ok, data));
            }
        }
        base.OnLoad(e);
    }

    protected virtual void OnMessageBoxShow(MessageBoxEventHandler e)
    {
        if (this.GetMessageBoxResponse != null)
        {
            this.GetMessageBoxResponse(this, e);
        }
    }

    protected override void Render(HtmlTextWriter output)
    {
        if (!base.DesignMode)
        {
            output.Write(this.content);
        }
        else
        {
            Label label = new Label();
            label.Font.Bold = this.Font.Bold;
            label.Font.Italic = this.Font.Italic;
            label.Font.Names = this.Font.Names;
            label.Font.Overline = this.Font.Overline;
            label.Font.Size = this.Font.Size;
            label.Font.Strikeout = this.Font.Strikeout;
            label.Font.Underline = this.Font.Underline;
            label.ForeColor = this.ForeColor;
            label.BackColor = this.BackColor;
            label.BorderColor = this.BorderColor;
            label.BorderStyle = this.BorderStyle;
            label.BorderWidth = this.BorderWidth;
            label.Text = this.ID;
            label.RenderControl(output);
        }
    }

    // Properties
    private string HiddenFieldName
    {
        get
        {
            return ("hidF" + this.ID);
        }
    }

    // Nested Types
    public delegate void Message(object sender, MessageBox.MessageBoxEventHandler e);

    public class MessageBoxEventHandler : EventArgs
    {
        // Fields
        public readonly Button ButtonPressed;
        public readonly object Data;

        // Methods
        public MessageBoxEventHandler(Button buttonPressed) : this(buttonPressed, null)
        {
        }

        public MessageBoxEventHandler(Button buttonPressed, object data)
        {
            this.ButtonPressed = buttonPressed;
            this.Data = data;
        }

        // Nested Types
        public enum Button
        {
            Ok,
            Cancel,
            NotPressed
        }
    }
}

 
 
 

}

 
 
 

 

抱歉!评论已关闭.