On Ch 14, page 511 - you wrote:
The previous chapter showed you how to modify the DetailsView to insert validation controls to its templates
However, the DetailsView created in Chapter 14 does not have any item templates associated with it (at least I don't think it does)
I added a requiredvalidator after the end tag for the DetailsView, but received an error when trying to browse to the web page:
Code:
<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DefaultMode="Insert" InsertMethod="DetailsView1_InsertItem" >
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:CommandField ShowInsertButton="true" ShowCancelButton="false" />
</Fields>
</asp:DetailsView>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a name" ControlToValidate="DetailsView1"></asp:RequiredFieldValidator>
</asp:Content>
The error was as follows:
Control 'DetailsView1' referenced by the ControlToValidate property of 'RequiredFieldValidator1' cannot be validated.
Do I need to add an <ItemTemplate> to do this correctly?
I did this and it seemed to work, but your example doesn't have this and I just want to make sure that I was okay to do what I did:
Code:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DefaultMode="Insert" InsertMethod="DetailsView1_InsertItem" >
<Fields>
<asp:TemplateField HeaderText="Name">
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Enter a name"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="true"
ShowCancelButton="false" />
</Fields>
</asp:DetailsView>