Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Required field validators for a datagrid

  Asked By: Carolina    Date: Mar 08    Category: Asp.net    Views: 1150

Can anyone suggest how to use Required field validators for a datagrid while
edit/update operations? For e.g. to prevent the user from updating a column
with a blank. I have tried doing something but it doesn't work. The code is
here below and you might suggest if I am doing anything wrong in this.

private void dgUser_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
TextBox[] t = new TextBox[3];

t[0] = (TextBox)(e.Item.Cells[1].Controls[0]);
Rfv = new RequiredFieldValidator();
Rfv.ControlToValidate = t[0].ID;
Rfv.Enabled = true;
Rfv.ErrorMessage = "Fields Cannot be Blank";
Rfv.Display = ValidatorDisplay.None;

Also I would like to know if I can put a limit on the maximum no. of
characters on the controls.



2 Answers Found

Answer #1    Answered By: Adalwen Fischer     Answered On: Mar 08

Are you using TemplateColumn? If yes, you could put  validators statically
into EditItemTemplate with the TextBox and you wouldn't need to do dynamical
adding of validators in that case.

About maximum  length, by default such validator does not exist, but TextBox
has MaxLength property that you can use to set the maximum allowed length.
Although I am not sure how it behaves with other browsers than IE. It's not
hard to create custom validation control or CustomValidator to validate the
input length either.

Answer #2    Answered By: Dylan Evans     Answered On: Mar 08

But not all are Template columns. Also I would get into
problems with the maximum  length, if people start entering accents like "a
caret over o" that single character would account for 3 chars. So, can't use

Didn't find what you were looking for? Find more on Required field validators for a datagrid Or get search suggestion and latest updates.