Does anyone know or can point me to a tutorial where i can find how to use the standard validation controls to validate the contents of an asp datagrid from within the on update command.
The problem is that this isn't valid because no ID's are assinged to the rendered text boxes in the edit item index row therefore the ID string is null:
Validator1.ControlToValidate = ((TextBox)DataGrid.SelectedItem.Cells[4].Controls[0]).ID
The ControlToValidate property needs to be set in the code as it will point to objects that don't exist at design time.
Does anyone know a way around this? I have tried using the handle UniqueID but this doesn't work.
Any help would be much appreciated
Cheers
MAssuming that your validator is in the same row as the textbox and the textbox has an ID value assigned, you can simply assign ControlToValidate to that ID.
The validator will search for the ID in that row (technically in the naming container) and if found, it will validate. If not found, its an error.
For example, this will work:
<itemTemplate>
<asp:TextBox id=TextBox1 runat=server />
<asp:RequiredFieldValidator id=RFV1 runat=server ControlToValidate="TextBox1"
ErrorMessage="Required" />
</itemTemplate
When the Update button is submitted, on the server side test for Page.IsValid = true before running your update code.
Peter, I think you meant to have that be the <EditItemTemplate>, not the <ItemTemplate>.
The <EditItemTemplate> contains the markup that is displayed when a row's index matches the DataGrid's EditItemIndex property value.
Is there a way to place the validation control, or at least the resulting error message outside of the datagrid? I have a couple of columns in my datagrid that are only 2 - 6 characters wide and when I place the validator in the <EditItemTemplate> the error message messes up the formatting. It forces the cell to grow to accomodate the possible error message and looks sloppy. I would really like for it to appear just outside the datagrid but the textbox it is validating is not valid outside of the <EditItemTemplate>.... or at least so it seems to me.
Thanks in advance for any help
John
This one is tricky. If you only have one row with edit textboxes, then you only need one validator for that row. If you had all rows in edit mode, then you need a lot of validators located below the grid.
The second problem is that each row of a datagrid is a separate "naming container". That imposes a restriction on validators. They require the ID in ControlToValidate to be in the same naming container. There is no workaround for this with the ASP.NET validators. However, my replacement validation framework,Professional Validation And More, is designed to handle this problem. You can assign an object reference for the textbox to the validator's ControlToEvaluate property programmatically (instead of using the ID in the ControlIDToEvaluate property.) My validation framework solves many other problems too. For example, it can easily format your error messages as an image that pops up an alert or tooltip with the error message or a small hyperlink that pops up an alert. These formats can use very little screen space. There are all kinds of other neat tools to really assist you within validation.
No comments:
Post a Comment