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