Apr 13 2006

Define a Custom Event inside UserControls

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 08:51

If you are working with either a new custom server control or a normal web user control, this is the proper way of adding an event that can be handled by the parent control of the child control you are develping:

Add the following to the control (Server Control / Web User Control ) being developed:

#region Event Handler 
private static readonly object EventBubbleRaised = new object(); 
public event EventHandler BubbleRaise 
{ 
       add 
      { 
                Events.AddHandler(EventBubbleRaised, value); 
       } 
       remove 
      { 
               Events.RemoveHandler(EventBubbleRaised, value); 
      } 
} 
protected virtual void OnBubbleClick(EventArgs e) 
{
              EventHandler BubbleRaised = (EventHandler)Events[EventBubbleRaised]; 
              if (BubbleRaised != null) 
                      BubbleRaised(this, e); 
} 
#endregion

In the parent page / control, you add this in the Init method:

BubbleControl.BubbleRaise += new EventHandler(ControlName_BubbleRaise);


Then you define you our handler:

private void ControlName_BubbleRaise(object sender, EventArgs e) 
{ 
                Response.Write(sender.GetType().ToString()); 
}

                

That was a simple example, but at least you know now the steps.

Regards

Tags: ,

Apr 13 2006

Handle MasterPages' Control Events in Content Page

Category: ASP.NET 2.0 - GeneralBil@l @ 06:42

Hi:

While working on a project having MasterPages, Content Pages, TreeView Controls, Themes, etc ... I was faced with the following situation:

In one of the master pages in my application, I had a TreeView, and a custom menu, in a sense, once a button is clicked in that menu, two things shall happen:

  • Redirect to another page
  • Populate the TreeView based on the page we are in now.

Redirecting is pretty simple, however populating the TreeView is not that simple, knowing that the TreeView is placed inside the Master Page.

Solution?

A simple solution would be to override the OnInit() method (in my base class of the application), get the TreeView control from the master page and then populate that TreeView or add any handler for its predefined events as follows:

protected override void OnInit(EventArgs e)
    {
        _tr = (TreeView)Master.FindControl("tvMainNavigation");

        if (_tr != null)
        {
            _tr.TreeNodePopulate += new TreeNodeEventHandler(_tr_TreeNodePopulate);
        }

        // Call Base OnInit()
        base.OnInit(e);
    }

First of all, I am finding the control inside the Master Page, then I am defining an event handler for the TreeNodePopulate event.

Then, in the OnPreRender method I add the following to populate the TreeView:

protected override void OnPreRender(EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (_tr != null)
            {
                PopulateRootLevel();
            }
        }

        // Call based Load methods
        base.OnPreRender(e);
    }

Why OnPreRender? I had to pass in some values in the Page_Load of each content page before populating the TreeView in the base class, that is why I figured out that the best place to populate the TreeView is in the OnPreRender of the base class.

In addition, as you saw above, you can handle any event for the TreeView found in themaster Page:

  • Get the control from the master page
  • In the OnInit() method, attach any event handler to be fired in the content page for that control in the master page.

Hope this helps,

Regards

Tags:

Mar 29 2006

DateTime Formatting in ASP.NET 2.0

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 09:33

This is a nice summarized sample code on how to format DateTime instances:

// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

using System;
class Sample 
{
    enum Color {Yellow = 1, Blue, Green};
    static DateTime thisDate = DateTime.Now;

    public static void Main() 
    {
// Store the output of the String.Format method in a string.
    string s = "";

    Console.Clear();

// Format a negative integer or floating-point number in various ways.
    Console.WriteLine("Standard Numeric Format Specifiers");
    s = String.Format(
        "(C) Currency: . . . . . . . . {0:C}\n" +
        "(D) Decimal:. . . . . . . . . {0:D}\n" +
        "(E) Scientific: . . . . . . . {1:E}\n" +
        "(F) Fixed point:. . . . . . . {1:F}\n" +
        "(G) General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(N) Number: . . . . . . . . . {0:N}\n" +
        "(P) Percent:. . . . . . . . . {1:P}\n" +
        "(R) Round-trip: . . . . . . . {1:R}\n" +
        "(X) Hexadecimal:. . . . . . . {0:X}\n",
        -123, -123.45f); 
    Console.WriteLine(s);

// Format the current date in various ways.
    Console.WriteLine("Standard DateTime Format Specifiers");
    s = String.Format(
        "(d) Short date: . . . . . . . {0:d}\n" +
        "(D) Long date:. . . . . . . . {0:D}\n" +
        "(t) Short time: . . . . . . . {0:t}\n" +
        "(T) Long time:. . . . . . . . {0:T}\n" +
        "(f) Full date/short time: . . {0:f}\n" +
        "(F) Full date/long time:. . . {0:F}\n" +
        "(g) General date/short time:. {0:g}\n" +
        "(G) General date/long time: . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(M) Month:. . . . . . . . . . {0:M}\n" +
        "(R) RFC1123:. . . . . . . . . {0:R}\n" +
        "(s) Sortable: . . . . . . . . {0:s}\n" +
        "(u) Universal sortable: . . . {0:u} (invariant)\n" +
        "(U) Universal sortable: . . . {0:U}\n" +
        "(Y) Year: . . . . . . . . . . {0:Y}\n", 
        thisDate);
    Console.WriteLine(s);

// Format a Color enumeration value in various ways.
    Console.WriteLine("Standard Enumeration Format Specifiers");
    s = String.Format(
        "(G) General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
        "(D) Decimal number: . . . . . {0:D}\n" +
        "(X) Hexadecimal:. . . . . . . {0:X}\n", 
        Color.Green);       
    Console.WriteLine(s);
    }
}
/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

*/


This samlpe code is taken from:

http://msdn2.microsoft.com/library/system.string.format.aspx

Tags: ,

Mar 19 2006

Protect Configuration Sections in Web.Config

Category: ASP.NET 2.0 - GeneralBil@l @ 19:36

As you know, mostly developersdo place the connection string inside the web.config file. Sometimes, we need to use a username/pwd in the connection string and not only use Trusted Connection. It is always recommended to protected those areas inside your Web.Config especially when they include sensitive data.
In ASP.NET 2.0, it is so easy to accomplish this. I am going to show you a method that will allow you to encrypt/decrypt any section inside your Web.Config file. This method is the result of my complete research on this topic, by reading several articles online, whose links are shown below: