Source Code

for file /App_Code/VEControls/Select.cs

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

// App_Code/VEControls/Select.cs
// Implementation of an extended select control.
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 29.09.2005 created by Matthias Hertel
// 14.09.2007 eventname attribute added and PageProperty removed.


namespace AJAXControls {

  /// <summary>Extended HTML-Select control with support for OpenAjax events.</summary>
  [ToolboxData("<{0}:Select runat='server'></{0}:Select>")]
  public class Select : HtmlSelect {

    private string _eventname = null; // the name of the eventname property

    /// <summary>name of the OpenAjax event that gets published and monitored </summary>
    public string eventname {
      get { return _eventname; }
      set { _eventname = value; }
    } // eventname


    protected override void OnPreRender(EventArgs e) {
      base.OnPreRender(e);

      if ((this.Name == null) || (this.Name.Length == 0))
        this.Name = this.ID;

      if ((this.Name == null) || (this.Name.Length == 0))
        throw new Exception("The AJAX Select must have an id or name attribut specfied.");

      if (Page.Header == null)
        throw new Exception("The <head> element of this page is not marked with runat='server'.");

      // register the JavaScripts includes without need for a Form.
      if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "CommonBehaviour")) {
        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CommonBehaviour", String.Empty);
        ((HtmlHead)Page.Header).Controls.Add(new LiteralControl("<script type='text/javascript' src='"
          + Page.ResolveUrl("~/controls/jcl.js")
          + "'><" + "/script>\n"));
      } // if

      if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "SelectBehavior")) {
        Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "SelectBehavior", String.Empty);
        ((HtmlHead)Page.Header).Controls.Add(new LiteralControl("<script type='text/javascript' src='"
          + Page.ResolveUrl("~/controls/select.js")
          + "'><" + "/script>\n"));
      } // if
    } // OnPreRender


    /// <summary>Render the elements of the control to the output HTML writer and attach the behavior.</summary>
    /// <param name="output">An System.Web.UI.HtmlTextWriter that represents the output stream to render HTML content on the client.</param>
    protected override void Render(HtmlTextWriter output) {
      Attributes.Add("eventname", this._eventname);
      base.Render(output);

      output.WriteLine(@"<script defer='defer' type='text/javascript'>jcl.LoadBehaviour('"
      + this.ClientID + @"', SelectBehaviour);</script>");
    }
  }
}


This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.