Controls and Events


Controls and Events

The PalindromeCheckerRenderedControl shows how to render differently depending upon the state of the Text property. While that's a very useful thing in itself, it's often helpful to also alert the host page to the fact that a palindrome was found. You can do this by exposing an event from the control.

Most of ASP.NET's standard server-side controls already support events. You've already seen how the Button control sends an event to the host page when it is clicked. You can actually do this type of thing with any control. Let's add a PalindromeFound event to the PalindromeCheckerRenderedControl.

Adding a PalindromeFound Event

  1. Open the PalindromeCheckerRenderedControl.cs file. To add a PalindromeFound event, type in the following line.

    public class PalindromeCheckerRenderedControl : WebControl {        public event EventHandler PalindromeFound; // public event //…  }

  2. Once hosts have subscribed to the event, they'll want to know when it happens. To do this, fire an event upon detecting a palindrome. The best place to do this is within the Text property's setter.

          public string Text       {          get          {             return text;          }          set          {             text = value;             if(this.CheckForPalindrome()) {                if (PalindromeFound != null)                {                   PalindromeFound(this, EventArgs.Empty);                }             }          }       }

    Rebuild the project.

  3. Now wire the event in the host page. Remove the current instance of the PalindromeCheckerRenderedControl from the page and drop a new instance on the page. This will refresh the CustomControlLib.DLL assembly so the changes (the new event) will appear in Visual Studio.

  4. Select the PalindromeCheckerRenderedControl on the page and click the Events button (the little lightning bolt) in the property page in Visual Studio.

    graphic

    Double-click on the text box next to the PalindromeFound event. Visual Studio will create an event handler for you.

  5. Respond to the PalindromeFound event.

    public partial class UsePalindromeCheckerControls : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {    }    protected void Button1_Click(object sender, EventArgs e)    {       this.PalindromeCheckerRenderedControl1.Text =                 this.TextBox1.Text;    }    protected void PalindromeCheckerRenderedControl1_PalindromeFound(                object sender, EventArgs e)    {       Response.Write("The page detected a PalindromeFound event");    } }

You should see something like the following when you type a palindrome:

graphic

Now that the control renders palindromes correctly and has an event, let's take a closer look at the parameter passed in during the call to Render: HtmlTextWriter.




Microsoft ASP. NET 2.0 Programming Step by Step
Microsoft ASP.NET 2.0 Step By Step (Step By Step (Microsoft))
ISBN: B002KE5VYO
EAN: N/A
Year: 2005
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net