Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : 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 March 16th, 2012, 11:26 AM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default Validation Controls not working in DetailsView

Hi,

I have created a DetailsView control that is supposed to add an item in the database. The control is attached to the datasource of a gridview just as it was done in the book. I have then converted two of its fields to a template to allow me replace them with other controls and add validation controls to them.

I have replaced one of the fields with a FileUpload control and another with a textbox. I removed the EditItem areas as it was done in the book. Now I added two validation controls; one for the textbox and the other for the FileUpload control.

After this, I try to test the application. When testing, I realized that the validation controls will activate when I try to edit an item in GridView, not the DetailsView. So I decided to add the validation controls to a ValidationGroup. Once I did this, the validatio controls no longer activate for the GridView, and they do not work for the DetailsView either.

Please help me out.

Thanks.
 
Old March 17th, 2012, 04:02 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Is this related to a specific page or exercise in the book? If so, can you specify page and chapter numbers and provide more detail on what exactly you're asking?

If it's not related, can you please post this in a more general ASP.NET category here: http://p2p.wrox.com/asp-net-4-539/ as you'll attract more viewers there. Also, be sure to post the full source code. If you provide a link, I'll take a look.

Cheers,

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!
 
Old March 21st, 2012, 12:51 AM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes it is related. In the book, you discussed how to use the validation controls within the server controls when the fields in the server controls have been converted to templates (Page 463 - Exercise titled "Customizing the DetailsView and Handling Its Events").

In this exercise, you demonstrate how we can add the Validation controls in the data controls.

Here is my entire code.

Code:
<%@ Page Title="Ruby Admin | Category" Language="C#" MasterPageFile="~/Masters/AdminMaster.master" AutoEventWireup="true" CodeFile="Categories.aspx.cs" Inherits="Categories" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CpReadable" Runat="Server">
    <div class="pageContent">
        <h1>
            Categories Administration
        </h1>
        <p>
            You can use this section to add and/or edit new or existing categories.
        </p>

        List of all Categories: 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" 
            EmptyDataText="There are no data records to display." AllowPaging="True" 
            AllowSorting="True" CssClass="gridView">
            <AlternatingRowStyle CssClass="alternatingRows" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="CategoryID" HeaderText="ID" ReadOnly="True" 
                    SortExpression="CategoryID" InsertVisible="False" />
                <asp:BoundField DataField="Category" HeaderText="Category" 
                    SortExpression="Category" />
                <asp:BoundField DataField="ImageUrl" HeaderText="Image Url" 
                    SortExpression="ImageUrl" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="DateAdded" HeaderText="Date Added" 
                    SortExpression="DateAdded" DataFormatString="{0:d}" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:RubyDBConnectionString %>" 
            DeleteCommand="DELETE FROM [Category] WHERE [CategoryID] = @CategoryID" 
            InsertCommand="INSERT INTO [Category] ([Category], [ImageUrl], [Description], [DateAdded]) VALUES (@Category, @ImageUrl, @Description, @DateAdded)" 
            ProviderName="<%$ ConnectionStrings:RubyDBConnectionString.ProviderName %>" 
            SelectCommand="SELECT [CategoryID], [Category], [ImageUrl], [Description], [DateAdded] FROM [Category]" 
            UpdateCommand="UPDATE [Category] SET [Category] = @Category, [ImageUrl] = @ImageUrl, [Description] = @Description, [DateAdded] = @DateAdded WHERE [CategoryID] = @CategoryID">
            <DeleteParameters>
                <asp:Parameter Name="CategoryID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="ImageUrl" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter DbType="DateTime2" Name="DateAdded" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="ImageUrl" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter DbType="DateTime2" Name="DateAdded" />
                <asp:Parameter Name="CategoryID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

        <br />



        <h1>Add New Category</h1>
        Fill out the below form to add a new Category.

        <br />
        <br />
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" DefaultMode="Insert" 
            Height="50px" Width="500px" GridLines="None" 
            oniteminserting="DetailsView1_ItemInserting">
            <Fields>
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
                <asp:TemplateField HeaderText="Category" SortExpression="Category">
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Category") %>' 
                            CssClass="textbox" Width="250px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                            ControlToValidate="TextBox1" CssClass="error" 
                            ErrorMessage="Category is a required field." ValidationGroup="addCategory">*</asp:RequiredFieldValidator>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ImageUrl" SortExpression="ImageUrl">
                    <InsertItemTemplate>
                        <asp:FileUpload ID="FileUpload1" runat="server" CssClass="textbox" 
                            Width="250px" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                            ControlToValidate="FileUpload1" CssClass="error" 
                            ErrorMessage="You have not selected an image to upload." 
                            ValidationGroup="addCategory">*</asp:RequiredFieldValidator>
                        <asp:CustomValidator ID="CustomValidator1" runat="server" CssClass="error" 
                            ErrorMessage="You have not selected a valid image file" 
                            onservervalidate="CustomValidator1_ServerValidate" 
                            ValidationGroup="addCategory">*</asp:CustomValidator>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("ImageUrl") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description" SortExpression="Description">
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Description") %>' 
                            CssClass="textbox" Height="150px" TextMode="MultiLine" Width="250px"></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:BoundField DataField="DateAdded" HeaderText="DateAdded" 
                    InsertVisible="False" SortExpression="DateAdded" />
                <asp:CommandField ButtonType="Button" InsertText="Add Category" 
                    ShowInsertButton="True" >
                <ControlStyle CssClass="button" />
                </asp:CommandField>
            </Fields>
        </asp:DetailsView>
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" CssClass="error" runat="server" />
    </div>
</asp:Content>
 
Old March 21st, 2012, 04:05 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Since it's *not* related to the Planet Wrox web site, but to a site you've built yourself, it's hard for me to test this code and see what's going on. There are a lot of dependencies on your database, connection strings and so on, stopping me from seeing this code work at run-time.

That said, could it be that you haven't set the ValidationGroup for the buttons in the DetailsView? Right now, you have a CommandField but you could convert that to a template field and set the ValidationGroup property on individual buttons or other controls you may use.

Cheers,

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!
 
Old March 21st, 2012, 11:37 PM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thank you. That was the problem. I set the ValidationGroup property for the validation controls but forgot to set it for the control that actually causes the validation.

That's again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Detailsview not working... leeWozyWarren BOOK: Beginning ASP.NET 4 : in C# and VB 4 November 28th, 2011 08:08 AM
Cascading dropdownlists not working in the edit template of detailsview thd_tq123 ASP.NET 3.5 Professionals 1 February 27th, 2009 07:55 AM
DetailsView update not working. Andrew.Berry ASP.NET 2.0 Professional 6 April 22nd, 2008 11:23 AM
Accessing server Controls embeded in DetailsView KittyKat .NET Framework 2.0 2 December 8th, 2006 03:22 PM
Validation Controls andyj00 ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 09:19 AM





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