Click here to Skip to main content
15,885,435 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionData Blocks Pin
Tiger4569-Jul-06 15:07
Tiger4569-Jul-06 15:07 
AnswerRe: Data Blocks Pin
Not Active9-Jul-06 16:16
mentorNot Active9-Jul-06 16:16 
QuestionForcing refresh of an HTML frame Pin
WebMaster9-Jul-06 15:03
WebMaster9-Jul-06 15:03 
AnswerRe: Forcing refresh of an HTML frame Pin
mnaveed9-Jul-06 22:25
mnaveed9-Jul-06 22:25 
GeneralRe: Forcing refresh of an HTML frame Pin
cloudking1196610-Jul-06 2:21
cloudking1196610-Jul-06 2:21 
QuestionASP.net MasterPages [modified] Pin
WebMaster9-Jul-06 13:13
WebMaster9-Jul-06 13:13 
QuestionQuery Strings and Frames Pin
JBL_Andon9-Jul-06 7:59
JBL_Andon9-Jul-06 7:59 
AnswerRe: Query Strings and Frames Pin
Guffa9-Jul-06 10:39
Guffa9-Jul-06 10:39 
Questionproved extra info for the membership? Pin
Snowjim9-Jul-06 6:56
Snowjim9-Jul-06 6:56 
QuestionCompiling 1.1 and 2.0 version of same library? Pin
rmatejka9-Jul-06 5:03
rmatejka9-Jul-06 5:03 
AnswerRe: Compiling 1.1 and 2.0 version of same library? Pin
RichardGrimmer11-Jul-06 5:26
RichardGrimmer11-Jul-06 5:26 
GeneralRe: Compiling 1.1 and 2.0 version of same library? Pin
rmatejka11-Jul-06 5:56
rmatejka11-Jul-06 5:56 
Questionevent in custom control [modified] Pin
g00fyman9-Jul-06 3:51
g00fyman9-Jul-06 3:51 
AnswerRe: event in custom control Pin
minhpc_bk9-Jul-06 16:45
minhpc_bk9-Jul-06 16:45 
GeneralRe: event in custom control Pin
g00fyman9-Jul-06 16:52
g00fyman9-Jul-06 16:52 
GeneralRe: event in custom control Pin
minhpc_bk9-Jul-06 17:13
minhpc_bk9-Jul-06 17:13 
GeneralRe: event in custom control Pin
g00fyman9-Jul-06 17:26
g00fyman9-Jul-06 17:26 
GeneralRe: event in custom control Pin
minhpc_bk9-Jul-06 18:24
minhpc_bk9-Jul-06 18:24 
GeneralRe: event in custom control Pin
g00fyman9-Jul-06 18:41
g00fyman9-Jul-06 18:41 
GeneralRe: event in custom control Pin
g00fyman9-Jul-06 19:05
g00fyman9-Jul-06 19:05 
GeneralRe: event in custom control Pin
g00fyman9-Jul-06 19:20
g00fyman9-Jul-06 19:20 
GeneralRe: event in custom control Pin
minhpc_bk9-Jul-06 19:32
minhpc_bk9-Jul-06 19:32 
GeneralRe: event in custom control [modified] Pin
g00fyman9-Jul-06 20:59
g00fyman9-Jul-06 20:59 
GeneralRe: event in custom control Pin
minhpc_bk10-Jul-06 17:11
minhpc_bk10-Jul-06 17:11 
Hi there,
Below is a custom MessageBox control which demontrates 3 things in a composite control:
+ Override the CreateChildControls method to add the child controls to the control hierarchy.
+ Override the Render method to control the appearance of the custom control at both design and runtime.
+ Override the OnBubbleEvent method to capture the button command event, and raise the custom Click event of the MessageBox control.

[DefaultProperty("Text")]
[DefaultEvent("Click")]
[ToolboxData("<{0}:MessageBox runat=server></{0}:MessageBox>")]
public class MessageBox : WebControl, INamingContainer
{
    private MessageBoxType m_MessageBoxType = MessageBoxType.YesNoCancel;
    private Button m_Button1 , m_Button2, m_Button3;
            
    [Category("Action"), Description("Fired when a message button is clicked")]
    public event MessageBoxEventHandler Click;
                
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("Message")]
    [Localizable(true)]
    public string Text
    {
        get
        {
            String s = (String)ViewState["Text"];
            return ((s == null) ? "Message" : s);
        }

        set
        {
            ViewState["Text"] = value;
        }
    }
            
    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return base.Controls;
        }
    }
              
    protected void OnClick(object sender, MessageBoxEventArgs e)
    {
        if (this.Click != null)
        {
            this.Click(this, e);
        }
    }
                  
    [DefaultValue(MessageBoxType.YesNoCancel)]
    public MessageBoxType MessageBoxType
    {
        get { return m_MessageBoxType; }
        set 
        {
            m_MessageBoxType = value;
                 
            EnsureChildControls();
                      
            if (m_MessageBoxType == MessageBoxType.AbortRetryIgnore)
                BuildMessageBoxButtons("Abort", "Retry", "Ignore", true, true, true);
            else if(m_MessageBoxType == MessageBoxType.OkCancel)
                BuildMessageBoxButtons("Ok", "Cancel", string.Empty, true, true, false);
            else 
                BuildMessageBoxButtons("Yes", "No", "Cancel", true, true, true);
        }
    }
                
    private void BuildMessageBoxButtons(string button1Text, string button2Text, string button3Text, bool button1Visible, bool button2Visible, bool button3Visible)
    {
        //Button1
        m_Button1.Text = button1Text;
        m_Button1.Visible = button1Visible;
        m_Button1.CommandArgument = button1Text;
        //Button2
        m_Button2.Text = button2Text;
        m_Button2.Visible = button2Visible;
        m_Button2.CommandArgument = button2Text;
        //Button3
        m_Button3.Text = button3Text;
        m_Button3.Visible = button3Visible;
        m_Button3.CommandArgument = button3Text;
    }
              
    protected override void CreateChildControls()
    {
        Controls.Clear();
                  
        //Button1
        m_Button1 = new Button();
        m_Button1.ID = "Button1";
        //Button2
        m_Button2 = new Button();
        m_Button2.ID = "Button2";
        //Button3
        m_Button3 = new Button();
        m_Button3.ID = "Button3";
                
        BuildMessageBoxButtons("Yes", "No", "Cancel", true, true, true);
                  
        this.Controls.Add(m_Button1);
        this.Controls.Add(m_Button2);
        this.Controls.Add(m_Button3);
    }
          
    protected override void Render(HtmlTextWriter writer)
    {
        EnsureChildControls();
               
        writer.Write(Text);
        writer.Write("<br/>");
        m_Button1.RenderControl(writer);
        m_Button2.RenderControl(writer);
        m_Button3.RenderControl(writer);
    }
              
    protected override bool OnBubbleEvent(object source, EventArgs e)
    {
        if (e is CommandEventArgs)
        {
            CommandEventArgs e1 = (CommandEventArgs)e;
            MessageBoxResult result = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), e1.CommandArgument.ToString());
            OnClick(this, new MessageBoxEventArgs(result));
            return false;
        }
        return true;
    }    
}
         
public delegate void MessageBoxEventHandler(object sender, MessageBoxEventArgs e);
              
public class MessageBoxEventArgs : EventArgs
{
    private MessageBoxResult m_MessageBoxResult;
           
    public MessageBoxEventArgs(MessageBoxResult result)
    {
        m_MessageBoxResult = result;
    }
            
    public MessageBoxResult MessageBoxResult
    {
        get { return m_MessageBoxResult; }
    }

}
             
public enum MessageBoxType
{
    YesNoCancel,
    AbortRetryIgnore,
    OkCancel
}
            
public enum MessageBoxResult
{
    None,
    Yes,
    No,
    Cancel,
    Abort,
    Retry,
    Ignore,
    Ok
}

GeneralRe: event in custom control Pin
g00fyman10-Jul-06 17:42
g00fyman10-Jul-06 17:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.