ASP.NET DataGrid Validation Problems

liunx

Guest
I have an ASP.NET page that pulls data from an XML file, and then edits or deletes that file...depending on what options the user selects from the DataGrid. Then there's an Insert option in the footer of each DataGrid's column.

I'm now trying to validate the data, and I was hoping to use the built-in ASP.NET validation controls. So I started with the "RequiredFieldValidator". I only placed this validation in the footerTemplate of one column, but everytime I try to delete some entries using the Update job the validation pop's up anyway.

I'm not sure why this is happening, since this one validation job is only placed within that FooterTemplate column. Here's a link to a txt file that contains all of the code:

NOTE: You'll need to view the source code
<!-- m --><a class="postlink" href="http://www.douglas-county.com/contacts_edit.txt">http://www.douglas-county.com/contacts_edit.txt</a><!-- m -->

So my question is, can you use the built-in "RequiredFieldValidator" within a DataGrid, or do you have to use another option? If you can do this, then what am I doing wrong? Thanks for any advice.I think you are going to have to do this by hand. When I say that I really mean run a few if statements when the code is fired after the user clicks the submit button and check to see if the feilds were filled out. You can do this with js too if you want to be fancy and let the user know before the page is resent. I am not sure how you would go about tweaking in the existing validation controls for something like this though, I do not think that it is possible.Hello,

ValidationControl fire each time , when the page post backed.

Regards,
vskumarThanks for the quick responses.
ValidationControl fire each time, when the page post backed.
Huh? I'm not quite understanding what your suggestion means. Please keep in mind that I'm a complete newbie to ASP.NET and VB.NET. So I'm not sure if you're suggesting that I add a "ValidationControl" to the script to fire each time the page loads, or that the "ValidationControl" is firing each time the page is posted back. Sorry.

I think you are going to have to do this by hand. When I say that I really mean run a few if statements when the code is fired after the user clicks the submit button and check to see if the feilds were filled out. You can do this with js too if you want to be fancy and let the user know before the page is resent. I am not sure how you would go about tweaking in the existing validation controls for something like this though, I do not think that it is possible.
I did try this option using Labels instead of the "RequiredFieldValidator", and it does work great (see referenced code below). But the only problem with that solution is that the labels can only appear at the bottom of the page, and not next to each field as I'd like for it to do. If there was a way to insert a label inside of the "ItemTemplate", then this solution would be fine with me.

Here's the code that I created that uses a small script with labels to validate a field in the DataGrid.

1) First, I added this code to the end of the doInsert statement:

If tadd2.text="" Then
error6.visible="True"
Else
error6.visible="False"
End If

Then, I added this label to the end of the page:
<asp:Label id="error6" runat="server" width="402px" visible="False" bordercolor="#FFC080" font-size="Smaller" font-bold="True" forecolor="Red" tooltip="Error! You must enter a department" backcolor="White" font-names="Arial">
Error! You must enter a department</asp:Label>

Ok, that's it. Any more suggestions would be great. Thanks for your help & patience:)Ok, I think that I've created a workaround that doesn't use the built-in "RequiredFieldValidator" from ASP.NET. This is how I did it:

1) I created a variable at the top of the page
Dim testVar as string
2) I added 2 lines to the if...else statement to give that variable 2 values
If tadd2.text="" Then
error6.visible="True"
testVar="No dept was entered" '<--NEW VARIABLE2
Else
error6.visible="False"
testVar="" '<--NEW VARIABLE2
End If

3) I entered this code just under the textbox inside of the DataGrid
<%Response.Write(testVar) %>

It works great because it displays the error just under the textbox, which is exactly what I wanted it to do. I'm now going to work on adding this solution to all of the fields in the DataGrid, creating other if...else statements for "range", "compare", etc., and adding these statements to the other edit types (i.e. Edit/Delete). I'll then save those scripts to a "snippets" folder, and use them in the future.

Hopefully with the next release of ASP.NET, these little flaky behaviors with their validators will be worked out. In the meantime, at least I came up with a fix. Thank you for all of your input. I greatly appreciate it.
 
Back
Top