Spacewalker
Spacewalker

Reputation: 571

asp.net - Listbox - InsertItemTemplate RequiredFieldValidator

I have a asp.net Listview and I have add a RequiredFieldValidator for edit operations in EditItemTemplate.

When I click "Edit" button and try to save the data with an empty CustomerNameTextBox then I get error "Please enter your name!"
This is OK

<%@ Page Language="C#" UnobtrusiveValidationMode="None" AutoEventWireup="true" CodeBehind="frmMain.aspx.cs" Inherits="DB_mit_GridView.frmMain" %>

<EditItemTemplate>
    <tr style="">
        <td>
            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
        </td>
        <td>
            <asp:Label ID="CustomerIDLabel1" runat="server" Text='<%# Eval("CustomerID") %>' />
        </td>
        <td>
            <asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
            <%-- CustomerNameTextBox must not be empty when editing an existing record
            <asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
        </td>

But I want to check for empty CustomerNameTextBox not only when editing an existing record but also when I insert a new record.
So I add RequiredFieldValidator for Insert operations in InsertItemTemplate :

<InsertItemTemplate>
    <tr style="">
        <td>
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
        </td>
        <td>
            <asp:TextBox ID="CustomerIDTextBox" runat="server" Text='<%# Bind("CustomerID") %>' />
        </td>
        <td>
            <asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
            <%-- CustomerNameTextBox must not be empty when insertig a new record
            <asp:RequiredFieldValidator runat="server" id="reqName2" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
        </td                    

But as soon as I add RequiredFieldValidator to InsertItemTemplate I get the message "Please enter your name!" immediately after start of then page.
I have not clicked on the "Insert" button - the message just appears without any click.

So how can I make sure that certain textboxes are not empty when editing or insertig ? (I have no code behind)

Upvotes: 0

Views: 219

Answers (1)

charan tej
charan tej

Reputation: 1054

You can add validation group for all textboxes and for Insert button

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup(v=vs.110).aspx

Upvotes: 0

Related Questions