Class DynamicListBox

A ListBox which can retrieve and persist any client-side changes to its Items collection.

Inheritance
System.Object
DynamicListBox
Implements
System.Web.UI.IPostBackDataHandler
System.Web.UI.INamingContainer
Namespace: Mediachase.Web.Console.Controls
Assembly: Mediachase.WebConsoleLib.dll
Version: 10.8.0
Syntax
public class DynamicListBox : ListBox, IPostBackDataHandler, INamingContainer
Remarks

In order for your client-side changes to be persisted, you must call new javascript methods on the ListBox. This Select element now has "Add" and "Remove" methods. The Add method takes the value, text, and optional index of the new ListItem. The Remove method takes the index of the ListItem to remove.

By default, the clientscript used by the control is emited directly into the page. In order to save bandwidth, it's possible to have the control to use a script reference instead, but this requires the following handler to be added to the httpHandlers section of web.config.

<httpHandlers>
	<add verb="*"
		path="MetaBuilders_WebControls_DynamicListBoxResourceHandler.axd"
		type="MetaBuilders.WebControls.DynamicListBoxResourceHandler,MetaBuilders.WebControls.DynamicListBox"
		validate="false"
	/>
</httpHandlers>
Examples

The following is an example of how to combine the DynamicListBox with client-script in your page.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="mb" Namespace="MetaBuilders.WebControls" Assembly="MetaBuilders.WebControls.DynamicListBox" %>
<script runat="server">

	protected void Page_Load( Object sender, EventArgs e ) {
		RegisterClientScriptBlock( MyList.ClientID, "<script>\r\nwindow.MyListId='" + MyList.ClientID + "';\r\n</" + "script>");
	}
    protected void MyList_ListChanged( Object sender, EventArgs e ) {
        if( MyList.SelectedIndex == -1 ) {
            Result.Text = "The SelectedIndex changed and now nothing on the list is selected";
        } else {
            Result.Text = "The SelectedIndex changed and is now '" + MyList.SelectedIndex + "'<br>The selected item is: " + MyList.SelectedItem.Value + "/" + MyList.SelectedItem.Text;
        }
    }

</script>
<html><body><form runat="server">

    <mb:DynamicListBox id="MyList" runat="server" OnSelectedIndexChanged="MyList_ListChanged" SelectionMode="Multiple" >
        <asp:ListItem value="normalItem1" text="normalItem1"></asp:ListItem>
        <asp:ListItem value="normalItem2" text="normalItem2"></asp:ListItem>
    </mb:DynamicListBox>

    <br><br>
    <a href="javascript:remove();" >Remove</a>
    <a href="javascript:add();" >Add</a>
    <script>
		function remove() {
			var list = DynamicListBox_FindControl(window.MyListId);
			var keepLooking = true;
			while ( keepLooking ) {
				list.Remove( list.options.selectedIndex );
				if ( list.options.selectedIndex < 0 ) {
					keepLooking = false;
				}
			}
		}

		function add() {
			var list = DynamicListBox_FindControl(window.MyListId);
			var generatedName = "newItem" + ( list.options.length + 1 );
			list.Add(generatedName,generatedName);
		}
    </script>

    <br><br>
    <asp:Button runat="server" text="Smack"/>

    <br><br>
    <asp:Label runat="server" id="Result" />

</form></body></html>

Constructors

DynamicListBox()

Declaration
public DynamicListBox()

Properties

Controls

Overridden to call EnsureChildControls.

Declaration
public override ControlCollection Controls { get; }
Property Value
Type Description
System.Web.UI.ControlCollection
Remarks

Supposedly should be done for all controls which override CreateChildControls

Methods

CreateChildControls()

Overridden to include the hidden input which tracks the client side changes.

Declaration
protected override void CreateChildControls()

OnItemsChanged(EventArgs)

Raises the ItemsChanged event.

Declaration
protected virtual void OnItemsChanged(EventArgs e)
Parameters
Type Name Description
System.EventArgs e

The System.EventArgs instance containing the event data.

OnPreRender(EventArgs)

Overridden to register client script.

Declaration
protected override void OnPreRender(EventArgs e)
Parameters
Type Name Description
System.EventArgs e

An System.EventArgs object that contains the event data.

RegisterArrayScript()

Register this DynamicListBox with the Page

Declaration
protected virtual void RegisterArrayScript()

RegisterClientScript()

Registers all the client script for the DynamicListBox

Declaration
protected virtual void RegisterClientScript()

RegisterLibraryScript()

Registers the library script for the DynamicListBox

Declaration
protected virtual void RegisterLibraryScript()

RegisterStartupScript()

Registers the DynamicListBox startup script

Declaration
protected virtual void RegisterStartupScript()

RenderEndTag(HtmlTextWriter)

Overridden to render children after the end tag.

Declaration
public override void RenderEndTag(HtmlTextWriter writer)
Parameters
Type Name Description
System.Web.UI.HtmlTextWriter writer

A System.Web.UI.HtmlTextWriter that represents the output stream to render HTML content on the client.

Events

ItemsChanged

The event that is raised when the user has changed the listbox's items collection..

Declaration
public event EventHandler ItemsChanged
Event Type
Type Description
System.EventHandler

Implements

System.Web.UI.IPostBackDataHandler
System.Web.UI.INamingContainer