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

Using TextBoxWatermark in a FormView (VB)

by Christian Wenz

Download Code or Download PDF

The TextBoxWatermark control in the AJAX Control Toolkit extends a text box so that a text is displayed within the box. When a user clicks into the box, it is emptied. If the user leaves the box without entering text, the prefilled text reappears. This is also possible within a FormView control.

Overview

The TextBoxWatermark control in the AJAX Control Toolkit extends a text box so that a text is displayed within the box. When a user clicks into the box, it is emptied. If the user leaves the box without entering text, the prefilled text reappears. This is also possible within a FormView control.

Steps

First of all, a data source is required. This sample uses the AdventureWorks database and the Microsoft SQL Server 2005 Express Edition. The database is an optional part of a Visual Studio installation (including express edition) and is also available as a separate download under https://go.microsoft.com/fwlink/?LinkId=64064. The AdventureWorks database is part of the SQL Server 2005 Samples and Sample Databases (download at https://www.microsoft.com/downloads/details.aspx?FamilyID=e719ecf7-9f46-4312-af89-6ad8702e4e6e&DisplayLang=en). The easiest way to set the database up is to use the Microsoft SQL Server Management Studio Express (https://www.microsoft.com/downloads/details.aspx?FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796&DisplayLang=en) and attach the AdventureWorks.mdf database file.

For this sample, we assume that the instance of the SQL Server 2005 Express Edition is called SQLEXPRESS and resides on the same machine as the web server; this is also the default setup. If your setup differs, you have to adapt the connection information for the database.

In order to activate the functionality of ASP.NET AJAX and the Control Toolkit, the ScriptManager control must be put anywhere on the page (but within the <form> element):

[!code-aspxMain]

   1:  <asp:ScriptManager ID="asm" runat="server" />

Then, add a data source to the page which supports the DELETE, INSERT and UPDATE SQL statements. If you are using the Visual Studio assistant to create the data source, mind that a bug in the current version does not prefix the table name (Vendor) with Purchasing. The following markup shows the correct syntax:

[!code-aspxMain]

   1:  <asp:SqlDataSource ID="sds" runat="server" ConnectionString="Data
   2:   Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True"
   3:   DeleteCommand="DELETE FROM [Purchasing].[Vendor] WHERE [VendorID] = @VendorID"
   4:   InsertCommand="INSERT INTO [Purchasing].[Vendor] ([Name]) VALUES (@Name)"
   5:   ProviderName="System.Data.SqlClient"
   6:   SelectCommand="SELECT [VendorID], [Name] FROM [Purchasing].[Vendor]"
   7:   UpdateCommand="UPDATE [Purchasing].[Vendor] SET [Name] = @Name WHERE [VendorID] = @VendorID">
   8:   <DeleteParameters>
   9:   <asp:Parameter Name="VendorID" Type="Int32" />
  10:   </DeleteParameters>
  11:   <UpdateParameters>
  12:   <asp:Parameter Name="Name" Type="String" />
  13:   <asp:Parameter Name="VendorID" Type="Int32" />
  14:   </UpdateParameters>
  15:   <InsertParameters>
  16:   <asp:Parameter Name="Name" Type="String" />
  17:   </InsertParameters>
  18:  </asp:SqlDataSource>

Remember the name (ID) of the data source, since it will be used in the DataSourceID property of the FormView control. The <InsertItemTemplate> section of the FormView contains a textbox which is extended by the TextBoxWatermarkExtender control. Make sure that the extender resides within the template and not outside of it.

[!code-aspxMain]

   1:  <div>
   2:   <asp:FormView ID="FormView1" runat="server" DataSourceID="sds" AllowPaging="True">
   3:   <ItemTemplate>
   4:   <%# Eval("Name") %>
   5:   <asp:LinkButton ID="btnNew" runat="server" CommandName="New" Text="Insert" />
   6:   <asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
   7:   <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" />
   8:   </ItemTemplate>
   9:   <EditItemTemplate>
  10:   <asp:TextBox ID="tbEdit" runat="server" Text='<%# Bind("Name") %>' />
  11:   <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />
  12:   </EditItemTemplate>
  13:   <InsertItemTemplate>
  14:   <asp:TextBox ID="tbNew" runat="server" Text='<%# Bind("Name") %>' />
  15:   <asp:LinkButton ID="btnInsert" runat="server" CommandName="Insert" Text="Insert" />
  16:   <ajaxToolkit:TextBoxWatermarkExtender ID="tbwe" runat="server"
  17:   TargetControlID="tbNew" WatermarkText=" Vendor name " />
  18:   </InsertItemTemplate>
  19:   </asp:FormView>
  20:  </div>

Now when the user changes into the insert mode of the FormView control, the text field for the new vendor is prefilled thanks to the TextBoxWatermarkExtender control. A click inside the textbox lets the filler text disappear.

The watermark in the field comes from the extender

The watermark in the field comes from the extender (Click to view full-size image)

Previous Next



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