"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Using the ColorPicker Control Extender (VB)

by Microsoft

ColorPicker is an ASP.NET AJAX extender that provides client-side color-picking functionality with UI in a popup control. It can be attached to any ASP.NET TextBox control. It.

The goal of this tutorial is to explain how you can use the AJAX Control Toolkit ColorPicker control extender. The ColorPicker control extender displays a popup dialog that enables you to select a color. The ColorPicker is useful whenever you want to provide an intuitive user interface for a user to pick a color.

Extending a TextBox Control with the ColorPicker Control Extender

Imagine, for example, that you want to create a website that enables visitors to create customized business cards. Visitors can enter the text for a business card and pick the color. The ASP.NET page in Listing 1 contains two TextBox controls named txtCardText and txtCardColor. When you submit the form, the selected values are displayed (see Figure 1).

Simple form for creating a business card

Figure 01: Simple form for creating a business card (Click to view full-size image)

Listing 1 - CreateCard.aspx

[!code-aspxMain]

   1:  <%@ Page Language="VB" %>
   2:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   3:   
   4:  <script runat="server">
   5:   
   6:      Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   7:          ' Show the panel
   8:          pnlResults.Visible = true
   9:          
  10:          ' Show the selected values
  11:          lblSelectedText.Text = txtCardText.Text             
  12:          lblSelectedColor.Text = txtCardColor.Text
  13:      End Sub
  14:  </script>
  15:   
  16:  <html xmlns="http://www.w3.org/1999/xhtml">
  17:  <head id="Head1" runat="server">
  18:      <title></title>
  19:  </head>
  20:  <body>
  21:      <form id="form1" runat="server">
  22:      <div>
  23:      
  24:          <h1>Create a Business Card</h1>
  25:      
  26:          <asp:Label 
  27:              ID="lblCardText" 
  28:              Text="Card Text"
  29:              AssociatedControlID="txtCardText"
  30:              runat="server" />
  31:          <br />
  32:          <asp:TextBox
  33:              ID="txtCardText"
  34:              Runat="server" />
  35:              
  36:          <br /><br />    
  37:      
  38:          <asp:Label 
  39:              ID="lblCardColor" 
  40:              Text="Card Color"
  41:              AssociatedControlID="txtCardColor"
  42:              runat="server" />
  43:          <br />
  44:          <asp:TextBox
  45:              ID="txtCardColor"
  46:              Runat="server" />
  47:              
  48:          <br /><br />
  49:          
  50:          <asp:Button
  51:              ID="btnSubmit"
  52:              Text="Submit"
  53:              Runat="server" OnClick="btnSubmit_Click" />
  54:   
  55:          <asp:Panel ID="pnlResults" Visible="false" runat="server">
  56:   
  57:              <h2>Your Selection</h2>
  58:   
  59:              Selected Card Text:
  60:              <asp:Label
  61:                  ID="lblSelectedText"
  62:                  Runat="server" />
  63:              <br />        
  64:              Selected Card Color:
  65:              <asp:Label
  66:                  ID="lblSelectedColor"
  67:                  Runat="server" />
  68:          
  69:          </asp:Panel>
  70:      
  71:      </div>
  72:      </form>
  73:  </body>
  74:  </html>

The form in Listing 1 works, but it does not provide a great user experience. The user has to type a color into the textbox. If the user wants a specialized color - for example, just the right shade of pea green - then the user must figure out the HTML color code without any help.

You can use the ColorPicker control extender to create a better user experience. The ColorPicker displays a color dialog when you move focus to a TextBox control (see Figure 2).

The ColorPicker Control Extender

Figure 02: The ColorPicker Control Extender (Click to view full-size image)

You need to complete two steps to use the ColorPicker control extender with the form in Listing 1:

  1. Add a ScriptManager control to the page
  2. Add the ColorPicker control extender to the page

Before you can use the ColorPicker, you must add a ScriptManager to your page. A good place to add the ScriptManager is right below the opening server-side <form> tag. You can drag the ScriptManager onto the page from the toolbox (the ScriptManager is located under the AJAX Extensions tab). Alternatively, you can type the following tag into Source View beneath the opening server-side form tag:

<asp:ScriptManager ID=???ScriptManager1??? runat=???server??? />

The easiest way to add the ColorPicker control extender to the page is in Design View. If you hover your mouse over the txtCardColor TextBox, a smart task option appears the enables you to add an extender (see Figure 3). If you pick this option, the Extender Wizard appears (see Figure 4).

Adding an extender

Figure 03: Adding an extender (Click to view full-size image)

Selecting a control extender with the Extender Wizard

Figure 04: Selecting a control extender with the Extender Wizard (Click to view full-size image)

You can pick the ColorPicker extender to extend the txtCardColor TextBox with the ColorPicker extender. Click OK to close the dialog.

After you make these changes, the source for the page looks like Listing 2.

Listing 2 - CreateCard.aspx (with ColorPicker)

[!code-aspxMain]

   1:  <%@ Page Language="VB" %>
   2:   
   3:  <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
   4:   
   5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   6:   
   7:  <script runat="server">
   8:   
   9:      Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  10:          ' Show the panel
  11:          pnlResults.Visible = true
  12:          
  13:          ' Show the selected values
  14:          lblSelectedText.Text = txtCardText.Text            
  15:          lblSelectedColor.Text = txtCardColor.Text
  16:      End Sub
  17:  </script>
  18:   
  19:  <html xmlns="http://www.w3.org/1999/xhtml">
  20:  <head id="Head1" runat="server">
  21:      <title></title>
  22:  </head>
  23:  <body>
  24:      <form id="form1" runat="server">
  25:      <div>
  26:          <asp:ScriptManager ID="ScriptManager1" runat="server" />
  27:          
  28:          <h1>Create a Business Card</h1>
  29:      
  30:          <asp:Label 
  31:              ID="lblCardText" 
  32:              Text="Card Text"
  33:              AssociatedControlID="txtCardText"
  34:              runat="server" />
  35:          <br />
  36:          <asp:TextBox
  37:              ID="txtCardText"
  38:              Runat="server" />
  39:          
  40:          <br /><br />    
  41:      
  42:          <asp:Label 
  43:              ID="lblCardColor" 
  44:              Text="Card Color"
  45:              AssociatedControlID="txtCardColor"
  46:              runat="server" />
  47:          <br />
  48:          <asp:TextBox
  49:              ID="txtCardColor"
  50:              AutoCompleteType="None"
  51:              Runat="server" />
  52:              
  53:          <cc1:ColorPickerExtender 
  54:              ID="txtCardColor_ColorPickerExtender"  
  55:              TargetControlID="txtCardColor"            
  56:              Enabled="True" 
  57:              runat="server">
  58:          </cc1:ColorPickerExtender>
  59:              
  60:          <br /><br />
  61:          
  62:          <asp:Button
  63:              ID="btnSubmit"
  64:              Text="Submit"
  65:              Runat="server" OnClick="btnSubmit_Click" />
  66:   
  67:          <asp:Panel ID="pnlResults" Visible="false" runat="server">
  68:   
  69:              <h2>Your Selection</h2>
  70:   
  71:              Selected Card Text:
  72:              <asp:Label
  73:                  ID="lblSelectedText"
  74:                  Runat="server" />
  75:              <br />        
  76:              Selected Card Color:
  77:              <asp:Label
  78:                  ID="lblSelectedColor"
  79:                  Runat="server" />
  80:          
  81:          </asp:Panel>
  82:      
  83:      </div>
  84:      </form>
  85:  </body>
  86:  </html>

Notice that the page now contains a ColorPickerExtender control that appears directly below the txtCardColor TextBox control. The ColorPickerExtender control extends the txtCardColor control so that it displays a color picker dialog.

Using a Button to Launch the Color Picker Dialog

The ColorPicker extender supports the following properties:

You can use these properties to customize how the color picker dialog is displayed and how the selected color is displayed. The page in Listing 3 illustrates how you can use several of these properties.

Listing 3 - CreateCardButton.aspx

[!code-aspxMain]

   1:  <%@ Page Language="VB" %>
   2:   
   3:  <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
   4:   
   5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   6:   
   7:  <script runat="server">
   8:   
   9:      Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  10:          ' Show the panel
  11:          pnlResults.Visible = true
  12:          
  13:          ' Show the selected values
  14:          lblSelectedText.Text = txtCardText.Text             
  15:          lblSelectedColor.Text = txtCardColor.Text
  16:      End Sub
  17:  </script>
  18:   
  19:  <html xmlns="http://www.w3.org/1999/xhtml">
  20:  <head id="Head1" runat="server">
  21:      <title></title>
  22:  </head>
  23:  <body>
  24:      <form id="form1" runat="server">
  25:      <div>
  26:          <asp:ScriptManager ID="ScriptManager1" runat="server" />
  27:          
  28:          <h1>Create a Business Card</h1>
  29:      
  30:          <asp:Label 
  31:              ID="lblCardText" 
  32:              Text="Card Text"
  33:              AssociatedControlID="txtCardText"
  34:              runat="server" />
  35:          <br />
  36:          <asp:TextBox
  37:              ID="txtCardText"
  38:              Runat="server" />
  39:              
  40:          <br /><br />    
  41:      
  42:          <asp:Label 
  43:              ID="lblCardColor" 
  44:              Text="Card Color"
  45:              AssociatedControlID="txtCardColor"
  46:              runat="server" />
  47:          <br />
  48:          <asp:TextBox
  49:              ID="txtCardColor"
  50:              AutoCompleteType="None"
  51:              Runat="server" />
  52:          <asp:Button
  53:              ID="btnPickColor"
  54:              Text="Pick Color"
  55:              Runat="server" />
  56:          <asp:Label
  57:              ID="lblSample"
  58:              Runat="Server"> Sample </asp:Label>        
  59:          <cc1:ColorPickerExtender 
  60:              ID="txtCardColor_ColorPickerExtender"  
  61:              TargetControlID="txtCardColor"
  62:              PopupButtonID="btnPickColor"
  63:              PopupPosition="TopRight"
  64:              SampleControlID="lblSample"            
  65:              Enabled="True" 
  66:              runat="server">
  67:          </cc1:ColorPickerExtender>
  68:              
  69:          <br /><br />
  70:          
  71:          <asp:Button
  72:              ID="btnSubmit"
  73:              Text="Submit"
  74:              Runat="server" OnClick="btnSubmit_Click" />
  75:   
  76:          <asp:Panel ID="pnlResults" Visible="false" runat="server">
  77:   
  78:              <h2>Your Selection</h2>
  79:   
  80:              Selected Card Text:
  81:              <asp:Label
  82:                  ID="lblSelectedText"
  83:                  Runat="server" />
  84:              <br />        
  85:              Selected Card Color:
  86:              <asp:Label
  87:                  ID="lblSelectedColor"
  88:                  Runat="server" />
  89:          
  90:          </asp:Panel>
  91:      
  92:      </div>
  93:      </form>
  94:  </body>
  95:  </html>

The page in Listing 3 includes a Pick Color button (see Figure 5). When you click this button, the color picker dialog appears above the TextBox. If you select a color from the dialog then the selected color appears as the background color of the lblSample Label control.

The ColorPicker PopupButtonID property is used to associate the Pick Color button with the ColorPicker extender. When you supply a value for the PopupButtonID property, the color picker dialog no longer appears when the target control has focus. You must click the button to display the dialog.

The SampleControlID property is used to associate a control that displays the selected color with the ColorPicker. The ColorPicker changes the background color of this control to the currently selected color.

Displaying the color picker dialog with a button

Figure 05: Displaying the color picker dialog with a button (Click to view full-size image)

Summary

In this tutorial, you learned how to use the ColorPicker control extender to display a popup color picker dialog. First, we examined how you can display the dialog when focus is moved to a TextBox control. Next, you learned how to create a button that displays the color picker dialog when the button is clicked.

Previous



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/colorpicker/using-the-colorpicker-control-extender-vb.htm
< THANKS ME>