Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 18th, 2017, 12:42 AM
Authorized User
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default Ch 14: Page 511 - Validating the DetailView

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>
 
Old April 18th, 2017, 07:42 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

>> Do I need to add an <ItemTemplate> to do this correctly?

Yep, that's how it's done as you need to refer to specific individual controls to validate. ASP.NET wouldn't know what to do when you told it to validate the entire DetailsView.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 9 - Validating User Input papadan BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 2 April 9th, 2017 05:18 PM
Ch 14: Try it Out p. 511 - error 'dependent property in a ReferentialConstraint' jayinatlanta BOOK: Beginning ASP.NET 4 : in C# and VB 2 November 6th, 2011 12:32 AM
ch-9 Validating user i/p. pg no-309 & 315 mr malik BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 February 17th, 2010 11:21 AM
Ch 14 Page 485 Try-It-Out problem ken evans BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 January 17th, 2010 07:23 AM
Errata: Page 511 drupalmeister BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 June 22nd, 2009 01:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.