 |
| ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 30th, 2004, 07:51 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Required Field Validator to a datagrid editt item
Tried this on a less advanced forum, but no reply so here it is:
I want to add a Required Field Validator to a Datagrid Edit Item. I know i could do this using a template column but i forgot to add this functionality in at the start so if i could just add the functionality to the existing boundcolums dynamically or in some other way it would be great.
The onyl way i have tried so far is to try and add the control when i fire a sub from the OnItemDataBound event of the datagrid.
the sub i fire contains the following code
If (E.Item.ItemType = ListItemType.EditItem) Then
Dim rqdTitle As New RequiredFieldValidator
E.Item.Cells(3).Controls.Add(rqdTitle)
rqdTitle.ControlToValidate = (E.Item.Cells(1).Controls(0).ID)
rqdTitle.Text = "*"
End If
E.Item.Cells(1).Controls(0).ID is the textbox xontrol in the edit item on the datagrid which i want to validate.
When i click edit on an item i get the "The ControlToValidate property of '' cannot be blank. " so its obviously not seeing the control i want to validate.
As i say i would rather not go through changing all my required fields into template columns, so if there is a way to do it to a databound it would be great to know,
cheers
|
|

August 30th, 2004, 01:09 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I'm still confused why you dont use your validator in EditItemTemplate ...
anyway change your code and try this...
Code:
rqdTitle.ControlToValidate = (e.Item.FindControl("yourTextBoxID").ID)
does this help?
--------------------------------------------
Mehdi.:)
|
|

August 30th, 2004, 02:46 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
I don't know if you can do it that way, because BoundColumn is handled by the data grid when editing. That may be an issue for you; and you may have to convert.
Try clicking edit in the data grid, then view the source. Do you see more than one control, or see a textbox control at all? Never tried that personally, so don't know.
Brian
|
|

August 31st, 2004, 05:41 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Changed Code to be:
If (E.Item.ItemType = ListItemType.EditItem) Then
Dim rqdTitle As New RequiredFieldValidator
E.Item.Cells(3).Controls.Add(rqdTitle)
rqdTitle.ControlToValidate = (E.Item.Cells(1).Controls(0).ClientID)
rqdTitle.Text = (E.Item.Cells(1).Controls(0).ClientID)
End If
It seems to pick up the right id for the text box but get the following:
Unable to find control id 'dgAreaList__ctl3__ctl0' referenced by the 'ControlToValidate' property of ''.
I think i will just need to return to the forms and change the itemdatabound columns to be template ones, this is probably the best practice way of doing this. Was just trying to save myself some work. Thanks for the help guys
|
|

August 31st, 2004, 11:22 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
E.Item.Cells(1).Controls(0).ClientID,its not a good idea,
you should work with server-ID not client-ID,
can you tell me again why you cant add your validator to EditItemTemplate?
--------------------------------------------
Mehdi.:)
|
|

September 1st, 2004, 02:17 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cheers Mehndi,
There doesnt appear to be an server id associated with the control at thjat point hence it was retunring a null value and givign the error discussed, i just found it interesting that it picked up the client id.
I had created all my grids with BoundColumn instead of making some TemplateColumn so i could not use the EditItem. I have now gone back and changed those i feel need validating tTemplateColumn andused EditItem,
thanks
Duncan
|
|

September 1st, 2004, 09:17 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
The server ID would be the ID field.
Brian
|
|

September 1st, 2004, 09:32 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cheers Brian,
i know i tried using the ID property but as i explained in my original message i got "The ControlToValidate property of '' cannot be blank." So it was obviously not picking up an id. Odd thing is when i look at the source of the client page the text box control in question does not have an id only a name?
|
|

September 1st, 2004, 11:29 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hey,
Well it probably wouldn't... This ID is generated from the INamingContainer interface ('dgAreaList__ctl3__ctl0'). I think your best (and maybe only solution) is to redesign.
Brian
|
|

September 2nd, 2004, 02:12 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cheers again Brian,
i was aware how the id was created, would have expected to be able to pick it up the way i was doing it. However i think this is one of these cases where best practise is to redesign so i have done so. Thanks to all who helped on this topic
|
|
 |