Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate a Gridview

I have a gridview that I want to validate when it is in edit mode. how do I do this?

Below is my Gridview and below that is my first attempt.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#999999" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
        Width="387px" DataKeyNames = "APPID" AllowPaging="True">
        <PagerSettings Mode="NextPreviousFirstLast" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <Columns>
            <asp:BoundField DataField="APPName" HeaderText="Application" 
                SortExpression="APPName" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
            <asp:CommandField AccessibleHeaderText="Edit" ButtonType="Image" 
                DeleteImageUrl="~/images/bttnDelete.gif" 
                EditImageUrl="~/images/bttnEdit.gif" HeaderText="Action" 
                ShowDeleteButton="True" ShowEditButton="True" 
                ShowHeader="True" CancelImageUrl="~/images/bttnCancel.gif" 
                UpdateImageUrl="~/images/bttnSave.gif" InsertVisible="False" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>

First Attempt

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowState = DataControlRowState.Edit Then

            Dim savebtn As ImageButton = DirectCast(e.Row.Cells(2).Controls(0), ImageButton)
            savebtn.ValidationGroup = "grd"

            'set up Name Textbox
            Dim txtname As TextBox = DirectCast(e.Row.Cells(0).Controls(0), TextBox)
            txtname.ValidationGroup = "grd"


            Dim reqval As New RequiredFieldValidator
            reqval.ID = "reqnam"
            reqval.ValidationGroup = "grd"

            reqval.ErrorMessage = "Application Name Cannot Be Empty"
            reqval.ControlToValidate = txtname.UniqueID

        End If
like image 701
Eric Avatar asked Dec 29 '22 18:12

Eric


1 Answers

You best bet is to convert the BoundField into a TemplateField and add the validation control to the EditItemTemplate. So your first column would become:

<asp:TemplateField HeaderText="Application" SortExpression="APPName">
    <EditItemTemplate>
        <asp:TextBox ID="txtApp" runat="server" Text='<%# Bind("APPName") %>'/>
        <asp:RequiredFieldValidator runat='server' ID='requiredApp' 
            ErrorMessage='Application Name Cannot Be Empty' ControlToValidate='txtApp' /> 
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="labelApp" runat="server" Text='<%# Bind("APPName") %>'/>
    </ItemTemplate>
</asp:TemplateField>
like image 124
Keltex Avatar answered Jan 05 '23 16:01

Keltex