1 Replies - 7558 Views - Last Post: 04 May 2010 - 04:41 PM Rate Topic: -----

#1 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Validating fields inside a listview?

Posted 04 May 2010 - 12:10 PM

Hi there,

I am trying to validate the fields inside a listview on the clientside however i get errors.
It can't seem to find the contols to validate, as the error message states:

"ServerError in Program /Labb6.
Couldnt find control-ID: firstNameTextBox which refers to the property ControlToValidate for firstNameRequiredFieldValidator."


I was wondering what i might have missed? (Application works 100% without validation btw so nothing wrong with that)
Validation is located in the bottom of the aspx file

Code for .aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Labb 6</title>
    <link rel="StyleSheet" href="style.css" media="screen" /> 
</head>
<body>
    <form id="form1" runat="server">
    
    <!-- Status och Rubrik -->
    <div>
    <p class="headline">Hantera Kontakter, Labboration 6</p>
    
    <!-- Felmeddelanden, status -->     
    <asp:Label Visible="false" ID="StatusMessageLabel" runat="server" EnableViewState="false"></asp:Label>
    
    <!-- Visning av tabell -->
    <div id="viewArea">
        
        <!-- Data källan för listviewn, konfigureras ihop med delete,insert,update metoder-->
        <asp:ObjectDataSource ID="ContactObjectDataSource" runat="server"  DataObjectTypeName="Contact"
            SelectMethod="GetContacts" DeleteMethod="DeleteContact" 
            InsertMethod="SaveContact" UpdateMethod="SaveContact" TypeName="Service" 
            ondeleted="ContactObjectDataSource_Deleted" 
            oninserted="ContactObjectDataSource_Inserted" 
            onupdated="ContactObjectDataSource_Updated">
        </asp:ObjectDataSource>
        
        <!-- Listview med tillhörande layout, itemtemplate, edittemplate -->
        <asp:ListView ID="ContactsListView" DataKeyNames="ContactID" InsertItemPosition="FirstItem" DataSourceID="ContactObjectDataSource" runat="server">
            <LayoutTemplate>
                <table>
                    <tr class="Table">
                        <th class="subTh">
                            Manage Contacts
                        </th>
                        <th>
                            Name
                        </th>
                        <th>
                            Last Name
                        </th>
                        <th>
                            Email Address
                        </th>
                    </tr>
                    <asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>   
                </table>
            </LayoutTemplate>
            
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:LinkButton CssClass="buttonDelete" ID="DeleteLinkButton" CommandName="Delete" Text="Delete" runat="server"></asp:LinkButton>   
                        <asp:LinkButton CssClass="buttonEdit" ID="EditLinkButton" CommandName="Edit" Text="Edit" runat="server"></asp:LinkButton>
                    </td>
                    <td>
                        <asp:Label CssClass="discriptiontext" ID="FirstNameLabel" Text='<%# Eval("firstName") %>' runat="server"></asp:Label>
                    </td>
                    <td>
                        <asp:Label CssClass="discriptiontext" ID="LastNameLabel" Text='<%# Eval("lastName") %>' runat="server" ></asp:Label>
                    </td>
                    <td>
                        <asp:Label CssClass="discriptiontext" ID="EmailLabel" Text='<%# Eval("email") %>' runat="server"></asp:Label>
                    </td>
                    
                </tr>
            </ItemTemplate>

            <InsertItemTemplate>
                <tr>
                    <td>
                        <asp:LinkButton CssClass="buttonEdit" ID="InsertLinkButton" runat="server" CommandName="Insert" Text="Add" />
                        <asp:LinkButton CssClass="buttonDelete" ID="CancelLinkButton" runat="server" CommandName="Cancel" Text="Clean" />
                    </td>
                    <td>
                        <asp:TextBox ID="firstNameTextBox" runat="server" Text='<%# Bind("firstName") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="lastNameTextBox" runat="server" Text='<%# Bind("lastName") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:LinkButton CssClass="buttonEdit" ID="UpdateLinkButton" runat="server" CommandName="Update" Text="Save" />
                        <asp:LinkButton CssClass="buttonDelete" ID="CancelLinkButton" runat="server" CommandName="Cancel" Text="Abort" />
                    </td>
                    <td>
                        <asp:TextBox ID="firstNameTextBox" runat="server" Text='<%# Bind("firstName") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="lastNameTextBox" runat="server" Text='<%# Bind("lastName") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="emailTextBox" runat="server" Text='<%# Bind("email") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
        </asp:ListView>  
    </div>
    
    <!-- Validering Client -->
<asp:ValidationSummary ID="ValidationSummary" runat="server" /> 
       
     <!-- Krav för ifyllda fält -->
    <asp:RequiredFieldValidator ID="firstNameRequiredFieldValidator" ControlToValidate="firstNameTextBox" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="lastNameRequiredFieldValidator" ControlToValidate="lastNameTextBox" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="emailRequiredFieldValidator" ControlToValidate="emailTextBox" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    
    <!-- Krav för antalet tillåtna tecken (min,max), även att det måste vara sträng som förnamn, efternamn -->
    <asp:RangeValidator ID="firstNameRangeValidator" Type="String" ControlToValidate="firstNameTextBox" runat="server" ErrorMessage="RangeValidator" MaximumValue="50" MinimumValue="2"></asp:RangeValidator>
    <asp:RangeValidator ID="lastNameRangeValidator" Type="String" ControlToValidate="lastNameTextBox" runat="server" ErrorMessage="RangeValidator" MaximumValue="50" MinimumValue="2"></asp:RangeValidator>
    <asp:RangeValidator ID="emailRangeValidator" ControlToValidate="emailTextBox" runat="server" ErrorMessage="RangeValidator" MaximumValue="50" MinimumValue="2"></asp:RangeValidator>
    
    <!-- Reguljärt uttryck för email -->
    <asp:RegularExpressionValidator ControlToValidate="emailTextBox" ID="emailRegularExpressionValidator" runat="server"
            ErrorMessage="RegularExpressionValidator" 
            ValidationExpression="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$">
    </asp:RegularExpressionValidator>
    
    </div>
    </form>
</body>
</html>



This post has been edited by heady89: 04 May 2010 - 12:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Validating fields inside a listview?

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Validating fields inside a listview?

Posted 04 May 2010 - 04:41 PM

Move the validators into the table cell with the control that it is supposed to validate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1