Search code examples
asp.netdetailsviewrequiredfieldvalidator

Textbox Outside DetailsView - RequiredFieldValidator Not Firing


all. This is officially my first post of StackOverflow. Great site, and has helped me often, but I cannot find the answer to this.

I have an asp.net form with a TextBox that has a RequiredFieldValidator. Following this is a DetailsView with Edit and Delete enabled. The Textbox is NOT inside the DetailsView. I need the RequiredFieldValidator to fire when I click the Edit and/or Delete commands.

Here is my code for the two controls. I can post it all with code behind but it is a lot to post if not needed.

TIA for your assistance.

Ali ........................................................................

<asp:TextBox ID="txtReason" runat="server" Width="500px"></asp:TextBox>         <asp:RequiredFieldValidator ID="rfvReason" runat="server"  ControlToValidate="txtReason" 
        ErrorMessage="Reason is required for Edit/Delete"></asp:RequiredFieldValidator>

<asp:DetailsView ID="dvCOHDetail" runat="server" Height="50px" Width="700px" DataKeyNames="RecID" DataSourceID="sdsCOHIncident" AutoGenerateRows="False" OnDatabound="dvCOHDetail_DataBound" AutoGenerateEditButton="False" AutoGenerateDeleteButton="False" OnItemUpdated="dvCOHDetail_ItemUpdated"  OnItemDeleting="dvCOHDetail_ItemDeleting">
   <Fields>
   <asp:TemplateField HeaderText="ACTION:">
    <ItemTemplate>
     <asp:LinkButton ID="LinkButton2" runat=server Text="Edit" CommandName="edit"/>
     <asp:LinkButton ID="LinkButton1" runat="server" Text="Delete" CommandName="delete" OnClientClick="return confirm ( 'Are you sure you want to delete this record?' )"/>
    </ItemTemplate>
   </asp:TemplateField> 

........................................................................


Solution

  • You have to group all the controls that you want to validate and that occurs validation. As you want to validate the TextBox on LinkButton click you have to set the ValidationGroup property of the TextBox that you want to validate and the LinkButtons that occurs the validation with a same value(any string).

    <asp:TextBox ID="txtReason" runat="server" Width="500px"></asp:TextBox>
    
    <asp:DetailsView ID="dvCOHDetail" runat="server" Height="50px" Width="700px" DataKeyNames="RecID" DataSourceID="sdsCOHIncident" AutoGenerateRows="False" OnDatabound="dvCOHDetail_DataBound" AutoGenerateEditButton="False" AutoGenerateDeleteButton="False" OnItemUpdated="dvCOHDetail_ItemUpdated"  OnItemDeleting="dvCOHDetail_ItemDeleting">
        <Fields>
            <asp:TemplateField HeaderText="ACTION:">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton2" runat="server" ValidationGroup="ValidationGroup1" Text="Edit" CommandName="edit"/>
                    <asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="ValidationGroup1" Text="Delete" CommandName="delete" OnClientClick="return confirm ( 'Are you sure you want to delete this record?' )"/>
                </ItemTemplate>
           </asp:TemplateField> 
        </Fields>
    </asp:DetailsView>
    
    <asp:RequiredFieldValidator ID="rfvReason" runat="server"  ControlToValidate="txtReason"
        ErrorMessage="Reason is required for Edit/Delete" ValidationGroup="ValidationGroup1" EnableClientScript="False"></asp:RequiredFieldValidator>