Avatar of bcombe
bcombe

asked on 

How to perform validation in an editable datagrid

Hello,
I am using visual studio 2003 with framework 1.1.  And a regular buit-in datagrid server control

I have an editable datagrid with the following structure  (3 columns)

colmun0=title   colmun1=value   colmun2=edit-button

When the user clicks the edit button the column 1 becomes a text box and they can edit the value by clicking the update button.  This mechanism works just fine however :

The problem that I am having is that I don't know how to validate the data that is entered.

My first approach was to drop a requirefieldvalidator control and try to assign the controlToValidate property programmatically when they click the update button

like this:
me.RequiredFieldValidator1.ControlToValidate="txt1"

I was hopping that the  single control  RequiredFieldValidator1 could handle the validation of all the text boxes in the grid because the statment me.RequiredFieldValidator1.ControlToValidate="txt1" will be inside the update_click method. So dynamically it will know what control to validate.

Needless to say that is not working something is missing.

The second approach that I use was to create create the validator programatically in the update_click handler such as this:

  Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand

        Dim RFV As New RequiredFieldValidator

        RFV.ControlToValidate = CType(e.Item.FindControl("txt1"), TextBox).ID

        RFV.Validate()

        If Me.IsValid Then
            UpdateDatasetFromGrid()
            Me.DataGrid1.EditItemIndex = -1

        End If

    End Sub

This is not working neither.

Can anybody knows how to accomplish this validation?  Some fields are require some fields are telephone or zips , some are dates.

Thanks







.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Rejojohny
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

why did adding a requiredvalidator to the textbox fail? u said that u had to write code to bind it to the textbox .. so ia ssume u have the columns as bound contro. .. instead just convert that column into a template columm (using the right click menu option.. there is a link in the property window) .. then in the edittemplatecolum just add the validator control and set the controlToValidate in the design time .. no need for any code ..

Rejo
Avatar of bcombe
bcombe

ASKER

First of all the the column is a templet column that has a textbox call txt1.  I cannot add the validator in the templet because I don't need the validator for every field , and some fields require diffrent validators like dates or telephone numbers.

So I need to assign the validators on the fly. By that I mean when the user wants to update a field as soon as they click the edit button a validator will be either create or assigned to the textbox "txt1" so went they click update the value will only be accepted if is valid.

That is my idea but I haven't be able to make it work because I lack  technical knowlege when handling controls dynamically.





Avatar of Rejojohny
Rejojohny
Flag of United States of America image

>>First of all the the column is a templet column that has a textbox call txt1.  I cannot add the validator in the templet because I don't need the validator for every field
what do i mean by every field? it will be added for only the control which you set in the controlToValidate property of the validator control ..

>>and some fields require diffrent validators like dates or telephone numbers.
so you can add different validator for to check all these conditions and set the controlToValidate property accordingly ..
Avatar of bcombe
bcombe

ASKER

what happens is that I have a Datagrid that is dynamically binded.  
The grid has 2 columns first column is "field name" second column is the respective value.

The number of rows depends on own the user fill in some previous information (some questions are optional) .

So lets say the grid as 30 rows.  Of those 30 rows 15 are required fields , 5 are dates,  2 are phone numbers, and 2 are zip.

How can I handle the validation? .     Can I handle the validation of the 15 require fields with a single requirevalidator control ?

Or how can create a validator on the fly to handle each row ?

Thanks







Avatar of Rejojohny
Rejojohny
Flag of United States of America image

so when the grid is shown are all the fields in all 30 rows available for editing or does the user have to click on a edit link in the row that he wants to edit?

in both scenario you can manage with a single validator .. add the validator in the column that needs to be validated .. attach the validator to the textbox using the controltovalidate .. if u r showing all the rows as editable .. then in the itemdatabound event .. check for the condition when you do not want the validator .. if the condition satisfies, then make that validator invisible ..

if the user has to click on the edit button to edit a row .. in the edititem (check the event name) event, check for the condition when you do not want the validator .. if the condition satisfies for the current row, then make that invisible using visible = false

Rejo
Avatar of bcombe
bcombe

ASKER

>The has to click a link for the edit text box to appear.

>And I cannot simply attach a validator to particular fields because the grid is populated in run time.

So that is the scenario a datagrid that is populated in run time, where some of the fields require validation and some not.

So the problem is how to create the appropriate validator dynamically?


Avatar of Rejojohny
Rejojohny
Flag of United States of America image

do not create the validator dynamically, but disable the existing validator dynamically using validator.visible =false ..
Avatar of bcombe
bcombe

ASKER

I like the idea of disabling the validator , I haven't tested yet , I am busy .

But I will test it and when I do I will assign the points.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo