Oct 20 2006

DropDownList and Dynamic Loading of Controls

Category:Bil@l @ 15:35

When you are working with Loading Controls Dynamically, you might face this problem:

Suppose you have a Request Form, where clients can send you several requests, and whenever the user wants to create a new request, he/she will click on a button to create a new row of controls (elements of a request), however you want this to be done dynamically!

Why dynamically? You don't know ahead of time, how many requests the user wants to send, so you leave him/her with an option to create a new request, where you load the controls forming the request dynamically.

A requirements might come up, in which you have a DropDownList in the request, this DDL should post back, and upon postback, according to the value selected, you want to enable/disable another control in the same request row!

Typically, all controls of a request, share the same row number, for example, controls of the first request, have the following IDs:

txtRequestNumber1, txtRequestDate1, etc ...

Controls of request number 2 are:

txtRequestNumber2, etc ...

So you need a way to know which DDL was selected! Typically, you can't know that as far as I know when working with Dynamic Loading of controls, how would you know the ddlValues1 was selected and not ddlValues2 was selected?

The way I solved this problem, is by creating a new DropDownList, that uses the OnSelectedIndexChanged (built-in event) to fire a new Delegate I have created whose EventArgs, holds the DropDownList itself, so now yuo have full access on the DropDownList that was selected, from its ClientID, you know to which request row it belongs, and accordingly, you can get the other control to enable/disable.

I hope I made myself clear in this post!

You just need to copy and past the code below into a new Class Library of your own, build it, then add it to your VS 2003 toolbox.

You will use the RASelectedIndexChanged event to attach a method to it, to do whatever action you want.

I haven't tested the code on VS 2005, you might feel free to do so and tell me if it works fine!

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Controls
{
/// <summary>
/// RASelectedIndexEventHandler Delegate
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>

public delegate void RASelectedIndexChangedEventHandler(object sender, RASelectedIndexChangedEventArgs e);

/// <summary>
/// Summary description for RADropDownList.
/// </summary>

[DefaultProperty("Text"),
  ToolboxData("&lt;{0}:RADropDownList runat=server></{0}:RADropDownList>")]
public class RADropDownList : System.Web.UI.WebControls.DropDownList
{
  // Override OnSelectedIndexChanged to fire our event
  protected override void OnSelectedIndexChanged(EventArgs e)
  {
   RASelectedIndexChangedEventArgs args = new RASelectedIndexChangedEventArgs(this);
   OnRASelectedIndexChanged(args);
  }
 
  // Define a new event for the filter command event
  private static readonly object EventSelectedIndexChanged = new object();

 
/// <summary>
  /// Filter Event used to add/remove delegate instances
  /// </summary>

  [Category("Extended GridView Properties")]
  public event RASelectedIndexChangedEventHandler RASelectedIndexChanged
  {
   add
   {
    base.Events.AddHandler(RADropDownList.EventSelectedIndexChanged, value);
   }
   remove
   {
    base.Events.RemoveHandler(RADropDownList.EventSelectedIndexChanged, value);
   }
  }

  protected virtual void OnRASelectedIndexChanged(RASelectedIndexChangedEventArgs e)
  {
   RASelectedIndexChangedEventHandler handler1 =
    (RASelectedIndexChangedEventHandler)base.Events[RADropDownList.EventSelectedIndexChanged];

   if (handler1 != null)
   {
    handler1(this, e);
   }
  }
}

/// <summary>
/// FilterCommandEventArgs Class, specifies a single property called
/// FilterExpression that contains the expression to filter the
/// GridView on.
/// </summary>

public class RASelectedIndexChangedEventArgs : EventArgs
{
  private RADropDownList ddl;
  public RADropDownList raDropDownList
  {
   get { return this.ddl; }
   set { this.ddl = value; }
  }

  public RASelectedIndexChangedEventArgs()
  {
  }

  public RASelectedIndexChangedEventArgs(RADropDownList ddl)
  {
   this.ddl = ddl;
  }
}
}

Hope this helps,

Regards

Tags:

Comments are closed