Programming Microsoft ASP.NET 4 - Dino Esposito [263]
In particular, you might want to check whether the ID being inserted already exists in the data source. You can use a CustomValidator control attached to the text box:
Text='<%# Bind("ID") %>' /> ControlToValidate="txtID" OnServerValidate="CustomValidator1_CheckID" /> The CustomValidator control fires a server-side event in which you can run code to validate the text in the input field. The server event is fired via a postback and has the following prototype: protected void CustomValidator1_CheckID(object source, ServerValidateEventArgs e) { string proposedCustomerID = e.Value; e.IsValid = CheckIfUsed(proposedCustomerID); } private bool CheckIfUsed(string proposedCustomerID) { var c = CustomerRepository.Load(proposedCustomerID); // The object is of type NoCustomer if no matching customer exists if (c is DAL.NoCustomer) return true; return false; } The Load method in the sample data access layer (DAL) used in this example supports the Special Case pattern. In other words, the method always returns a valid Customer object regardless of the value of the input proposedCustomerID parameter. If a customer with a matching ID can’t be found, the return object is an instance of the NoCustomer class. Of course, NoCustomer is a class that derives from Customer. How is this different from returning a plain null value or perhaps an error code? In both cases, the caller can figure out whether a matching customer exists or not. However, returning a special-case Customer object is inherently more informative and doesn’t violate the consistency of the method—a class that inherits from Customer is always returned, whereas an error code is a number and null is a non-value. Selecting an Item Triggering the Selection <%# Eval("Street") %>, <%# Eval("City") %>, <%# Eval("Country") %> <%# Eval("CompanyName") %> <%# Eval("Street") %>, <%# Eval("City") %>, <%# Eval("Country") %> In addition to changing some visual settings, the selected item template can contain buttons to trigger operations on the particular item. In Figure 11-10 shown earlier, each item features its own set of operational buttons, such as Edit and Delete. This layout can be reworked to display buttons only on the selected item. To do so, you just move the buttons to the SelectedItemTemplate property.
The SelectedItemTemplate property allows you to assign a different template to the currently selected item in the ListView control. Note that only one displayed item at a time can be given the special selected template. But how do you select an item?
The selected item template is a special case of the item template. The two templates are similar and differ mostly in terms of visual settings—for example, a different background color. The switch between the regular and selected item template occurs when the user clicks on a button with the Select command name. If you intend to support the selection item feature, you place a Select button in the item template. When this button gets clicked, the ListView automatically applies the new template to the clicked item. Here are some sample item and selected item templates: