Monday, March 26, 2012

Validating date in gridview

I am using the following code to validate a date in a Gridview. The error message gets displayed on an invalid date but a system error message immediately follows indicating that there was an error moving the data to a date field. Any suggestions on why I need to do to get the Gridview not to update the record when the validation fails?
Thanks...

<asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True"

AutoGenerateColumns="False"DataSourceID="zCaseType"BackColor="LightGoldenrodYellow"BorderColor="Tan"BorderWidth="1px"CellPadding="2"CellSpacing="5"DataKeyNames="CaseTypeId"ForeColor="Black"GridLines="None"HorizontalAlign="Left"Width="600px">

<Columns>

<asp:CommandFieldShowDeleteButton="True"ShowEditButton="True"/>

<asp:BoundFieldDataField="CaseTypeID"HeaderText="Id"

ReadOnly="True"SortExpression="CaseTypeID"/>

<asp:BoundFieldDataField="CaseTypeDescription"HeaderText="Description"

SortExpression="CaseTypeDescription"/>

<asp:TemplateFieldHeaderText="Inactive Date">

<ItemTemplate>

<asp:TextBoxID="InactiveDate"Text='<%#Bind("InactiveDate", "{0:d}")%>'runat="server"BackColor="LightGoldenrodYellow"/>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBoxID="InactiveDate"Text='<%#Bind("InactiveDate", "{0:d}")%>'runat="server"/>

<asp:CompareValidatorID="CompareValidator"ValidationGroup="GridView1"ControlToValidate="InactiveDate"errormessage="Invalid Date"Display=DynamicOperator=DataTypeCheckType=Daterunat="Server"/>

</EditItemTemplate>

</asp:templateField>

<asp:CommandFieldCausesValidation="true"ValidationGroup="GridView1"/>

</Columns>

<FooterStyleBackColor="Tan"/>

<PagerStyleBackColor="PaleGoldenrod"ForeColor="DarkSlateBlue"HorizontalAlign="Center"/>

<SelectedRowStyleBackColor="DarkSlateBlue"ForeColor="GhostWhite"/>

<HeaderStyleBackColor="Tan"Font-Bold="True"Width="30px"/>

<AlternatingRowStyleBackColor="PaleGoldenrod"/>

</asp:GridView>

Problem was I didn't have the validation tied to the edit button. When I added the following line everything worked fine.

<asp:CommandFieldValidationGroup="GridView1"CausesValidation="true"ShowEditButton="True"ShowDeleteButton=True/>

No comments:

Post a Comment