Saturday, March 24, 2012

Validation in gridview

i m using a gridview and i want validation on the textbox i m using in item templet at serverside, evertime my validation summery control is not working it is not giving any validation msg, can anybody solve this problem that how i use validationsummery control in gridview validation

hi,

if you want to place a validator inside the gridview and use validation summary for that, use the following steps else please explain your problem if i am wrong

place the requiredfield validator besides the textbox in the gridview item template.

Drag aValidationSummary control to the form on which to display the error messages. Place theValidationSummary control where you want to display the collected error messages on a target device.

In the Properties window, set the formtoValidate property to the ID of the form for which you want to summarize error messages.


Maybe I shouldn't ask my question in this thread, but it is virtually identical to the OP's so I figured any answers would be helpful to us both.

So, I am doing basically the same, I have a textbox in an ItemTemplate and Compare validator directly below it (checking that an integer was entered). Underneath the grid I have a ValidationSummary. If I enter a non-integer in the textbox, the ErrorMessage displays within the CompareValidator in the grid, instead of showing on the ValidationSummary. Here's my code:

 <asp:TemplateField HeaderText="Qty"> <ItemTemplate> <asp:TextBox Text='<%# Bind("Quantity")%>' onChange='EnableEntrySave()' Width="100%" ID="txtQuantity" runat="server"> </asp:TextBox> <asp:CompareValidator ID="valQtyNumeric" runat="server" ControlToValidate="txtQuantity" Display="Dynamic" SetFocusOnError="true" Text="" ErrorMessage="Error: Qty must be a number!" Operator="DataTypeCheck" Type="Integer"> </asp:CompareValidator> </ItemTemplate> <ItemStyle Width="10%" /> </asp:TemplateField>

And for the ValidationSummary:

 <td> <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" EnableViewState="False" Width="100%" /> </td>

Hi ,

Give display="none " for CompareValidator and ShowMessageBox="true" for ValidationSummary

Regards,

Naveen


What proved to be the issue for me was that on form load I had my submit button disabled. When I had it enabled by default on load, the validation summary worked. Here's what I finally ended up with (hope this helps the original OP)

 <asp:TemplateField HeaderText="Qty"> <ItemTemplate> <asp:TextBox Text='<%# Bind("Quantity")%>' Width="100%" ID="txtQuantity" runat="server"> </asp:TextBox> <asp:CompareValidator ID="valQtyNumeric" runat="server" ControlToValidate="txtQuantity" Display="none" Text="*" ErrorMessage='Error: Qty for any Work Performed must be a number!' Operator="DataTypeCheck" Type="Integer"> </asp:CompareValidator> </ItemTemplate> <ItemStyle Width="10%" /> </asp:TemplateField><asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="false" ShowMessageBox="true" Width="100%" />

No comments:

Post a Comment