Add a dynamically created AsyncControl
Dynamically Create: AsyncWindowAsyncLinkButtonAsyncButton
The AsyncPlaceHolder provides you with fast, and lightweight dynamic control creation.
<%@ Register Assembly="AsyncControls" Namespace="DelvingWare.AsyncControls" TagPrefix="dw" %> <dw:AsyncLabel runat="server" ID="lblMain" RenderMode="Paragraph" CssClass="placeHolderLabel">Add a dynamically created AsyncControl</dw:AsyncLabel> <dw:AsyncPlaceHolder runat="server" ID="phMain" /> <p> <dw:AsyncButton runat="server" ID="btAdd" OnClick="btAdd_Click" CausesValidation="true" Text="Add AsyncControl" CssClass="greyButton" /> <dw:AsyncButton runat="server" ID="btRemove" OnClick="btRemove_Click" CausesValidation="true" Text="Remove AsyncControl" CssClass="greyButton"/> </p> <p> Dynamically Create: <dw:AsyncDropDownList runat="server" ID="drpMain" EnableValidation="enabled" InitialValue="none" ErrorMessageCssClass="errorMsg" ErrorMessage="Please select a valid control."> <dw:AsyncListItem Value="none" /> <dw:AsyncListItem Value="window">AsyncWindow</dw:AsyncListItem> <dw:AsyncListItem Value="link">AsyncLinkButton</dw:AsyncListItem> <dw:AsyncListItem Value="button">AsyncButton</dw:AsyncListItem> </dw:AsyncDropDownList> </p>
using System; using DelvingWare.AsyncControls; ... protected void btAdd_Click( object sender, AsyncEventArgs ae ) { // ensure validation passed if ( !AsyncPage.IsValid ) return; int cCount = phMain.Controls.Count+1; // check the control collection count if ( phMain.Controls.Count == 5 ) { // display the restriction to the user AsyncPage.Alert("This demo only allows 5 controls to be created."); return; } switch ( drpMain.SelectedValue ) { case "window": // get the number of AsyncWindows in the place holder int wndCount = phMain.Controls.FindControlsByType(typeof(AsyncWindow)).Length; if ( wndCount == 2 ) { AsyncPage.Alert("This demo only allows 2 AsyncWindows to be created."); return; } // create the AsyncWindow and set the values AsyncWindow wnd = new AsyncWindow(); // <-- generate unique ID wnd.ID = "wnd"+ ((byte)DateTime.Now.Ticks); wnd.Title = "New AsyncWindow "+(wndCount+1); wnd.DefaultStyle = false; wnd.CssClass = "asyncWin2"; wnd.TitleBarCssClass = "titleBar2"; wnd.TitleCssClass = "titleBarText2"; wnd.TitleBarButtonHeight = 28; wnd.TitleBarButtonWidth = 42; wnd.ShadowCssClass = "awShadow"; wnd.Height = 300; wnd.Width = 200; wnd.Top = 400; wnd.Left = 150*cCount; wnd.Closed += new AsyncEventHandler(wnd_Closed); wnd.CloseIconUrl = "../images/vistaClose.gif"; wnd.AllowCollapsing = false; // add the control to the AsyncPlaceHolder phMain.Controls.Add(wnd); break; case "button": // create the AsyncButton and set the values AsyncButton bt = new AsyncButton(); bt.ID = "bt"+ cCount; bt.Text = "New AsyncButton "+ cCount; bt.Click += new AsyncEventHandler(bt_Click); // add the control to the AsyncPlaceHolder phMain.Controls.Add(bt); break; case "link": // create the AsyncLinkButton and set the values AsyncLinkButton lnk = new AsyncLinkButton(); lnk.ID = "lnk"+ cCount; lnk.Text = "New AsyncLinkButton "+ cCount; lnk.Click += new AsyncEventHandler(lnk_Click); // add the control to the AsyncPlaceHolder phMain.Controls.Add(lnk); break; } // end SWITCH // update the client side AsyncPlaceHolder phMain.UpdatePlaceHolder(); } void wnd_Closed( object sender, AsyncEventArgs ae ) { // remove the control from the AsyncPlaceHolder phMain.Controls.Remove(sender as IAsyncWidget); // check the tag value if ( btRemove.Tag == "remove" ) { // update the client side AsyncPlaceHolder phMain.UpdatePlaceHolder(); btRemove.Enabled = true; // <-- enable the remove button // display the notification to the user AsyncPage.MessageBox("Removed "+ (sender as AsyncWindow).Title, 3000); // clear tag btRemove.Tag = string.Empty; return; } // display the notification to the user AsyncPage.MessageBox("Closed "+ (sender as AsyncWindow).Title, 3000); } void lnk_Click( object sender, AsyncEventArgs ae ) { // check the tag value if ( btRemove.Tag == "remove" ) { // remove the control phMain.Controls.Remove(sender as IAsyncWidget); phMain.UpdatePlaceHolder(); btRemove.Enabled = true; // clear tag btRemove.Tag = string.Empty; return; } // update the client side label lblMain.Text = "Clicked "+ (sender as AsyncLinkButton).Text; } void bt_Click( object sender, AsyncEventArgs ae ) { if ( btRemove.Tag == "remove" ) { // remove the control phMain.Controls.Remove(sender as IAsyncWidget); phMain.UpdatePlaceHolder(); btRemove.Enabled = true; // clear tag btRemove.Tag = string.Empty; return; } // update the client side label lblMain.Text = "Clicked "+ (sender as AsyncButton).Text; } protected void btRemove_Click( object sender, AsyncEventArgs ae ) { // set the tag value btRemove.Tag = "remove"; // disable the button btRemove.Enabled = false; // display some instructions (for 5 seconds) AsyncPage.MessageBox("Click on a AsyncControl you want to remove. <p/>"+ "Remove an AsyncWindow by closing it (click the title bar). <br/>"+ "Remove an AsyncLinkButton or AsyncButton by clicking it.", 5000); }
Imports System Imports DelvingWare.AsyncControls ... Protected Sub btAdd_Click(ByVal sender As Object, ByVal ae As AsyncEventArgs) ' ensure validation passed If Not AsyncPage.IsValid Then Return End If Dim cCount As Integer = (phMain.Controls.Count + 1) ' check the control collection count If (phMain.Controls.Count = 5) Then ' display the restriction to the user AsyncPage.Alert("This demo only allows 5 controls to be created.") Return End If Select Case (drpMain.SelectedValue) Case "window" ' get the number of AsyncWindows in the place holder Dim wndCount As Integer = phMain.Controls.FindControlsByType(GetType(AsyncWindow)).Length If (wndCount = 2) Then AsyncPage.Alert("This demo only allows 2 AsyncWindows to be created.") Return End If ' create the AsyncWindow and set the values Dim wnd As AsyncWindow = New AsyncWindow ' <-- generate unique ID wnd.ID = ("wnd" + CType(DateTime.Now.Ticks,Byte)) wnd.Title = ("New AsyncWindow " _ + (wndCount + 1)) wnd.DefaultStyle = false wnd.CssClass = "asyncWin2" wnd.TitleBarCssClass = "titleBar2" wnd.TitleCssClass = "titleBarText2" wnd.TitleBarButtonHeight = 28 wnd.TitleBarButtonWidth = 42 wnd.ShadowCssClass = "awShadow" wnd.Height = 300 wnd.Width = 200 wnd.Top = 400 wnd.Left = (150 * cCount) AddHandler wnd.Closed, AddressOf Me.wnd_Closed wnd.CloseIconUrl = "../images/vistaClose.gif" wnd.AllowCollapsing = false ' add the control to the AsyncPlaceHolder phMain.Controls.Add(wnd) Case "button" ' create the AsyncButton and set the values Dim bt As AsyncButton = New AsyncButton bt.ID = ("bt" + cCount) bt.Text = ("New AsyncButton " + cCount) AddHandler bt.Click, AddressOf Me.bt_Click ' add the control to the AsyncPlaceHolder phMain.Controls.Add(bt) Case "link" ' create the AsyncLinkButton and set the values Dim lnk As AsyncLinkButton = New AsyncLinkButton lnk.ID = ("lnk" + cCount) lnk.Text = ("New AsyncLinkButton " + cCount) AddHandler lnk.Click, AddressOf Me.lnk_Click ' add the control to the AsyncPlaceHolder phMain.Controls.Add(lnk) End Select ' end SWITCH ' update the client side AsyncPlaceHolder phMain.UpdatePlaceHolder End Sub Private Sub wnd_Closed(ByVal sender As Object, ByVal ae As AsyncEventArgs) ' remove the control from the AsyncPlaceHolder phMain.Controls.Remove(CType(sender,IAsyncWidget)) ' check the tag value If (btRemove.Tag = "remove") Then ' update the client side AsyncPlaceHolder phMain.UpdatePlaceHolder btRemove.Enabled = true ' <-- enable the remove button ' display the notification to the user AsyncPage.MessageBox(("Removed " + CType(sender,AsyncWindow).Title), 3000) ' clear tag btRemove.Tag = string.Empty Return End If ' display the notification to the user AsyncPage.MessageBox(("Closed " + CType(sender,AsyncWindow).Title), 3000) End Sub Private Sub lnk_Click(ByVal sender As Object, ByVal ae As AsyncEventArgs) ' check the tag value If (btRemove.Tag = "remove") Then ' remove the control phMain.Controls.Remove(CType(sender,IAsyncWidget)) phMain.UpdatePlaceHolder btRemove.Enabled = true ' clear tag btRemove.Tag = string.Empty Return End If ' update the client side label lblMain.Text = ("Clicked " + CType(sender,AsyncLinkButton).Text) End Sub Private Sub bt_Click(ByVal sender As Object, ByVal ae As AsyncEventArgs) If (btRemove.Tag = "remove") Then ' remove the control phMain.Controls.Remove(CType(sender,IAsyncWidget)) phMain.UpdatePlaceHolder btRemove.Enabled = true ' clear tag btRemove.Tag = string.Empty Return End If ' update the client side label lblMain.Text = ("Clicked " + CType(sender,AsyncButton).Text) End Sub Protected Sub btRemove_Click(ByVal sender As Object, ByVal ae As AsyncEventArgs) ' set the tag value btRemove.Tag = "remove" ' disable the button btRemove.Enabled = false ' display some instructions (for 5 seconds) AsyncPage.MessageBox(("Click on a AsyncControl you want to remove. <p/>" + ("Remove an AsyncWindow by closing it (click the title bar). <br/>" + "Remove an AsyncLinkButton or AsyncButton by clicking it.")), 5000) End Sub
.placeHolderLabel { color: #777; font-family: Verdana, Arial, Serif; } .msgBox { font-size: 10pt; padding: .5em; background-color: White; border: 1px solid black; position: absolute; left: 850px; filter:alpha(opacity=85); -moz-opacity:.75;opacity:.85; }