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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0