-Dman100-
asked on
Gridview - reference textbox inside EditItemTemplate
I'm writing a custom validator method that for a textbox that inside a Gridview EditItemTemplate.
I'm invoking the validation at runtime when the RowDataBound event fires.
How can I access the textbox field inside the GridView EditItemTemplate. I was trying findcontrol, but that isn't working.
protected void ValidateInputLength(object source, ServerValidateEventArgs args)
{
if (GridView1.FindControl("tx tDescripti on").Text. Length > 255)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
I'm not able to access the text property and length method in the following line:
if (GridView1.FindControl("tx tDescripti on").Text. Length > 255)
Any help is appreciated.
Thanks.
I'm invoking the validation at runtime when the RowDataBound event fires.
How can I access the textbox field inside the GridView EditItemTemplate. I was trying findcontrol, but that isn't working.
protected void ValidateInputLength(object
{
if (GridView1.FindControl("tx
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
I'm not able to access the text property and length method in the following line:
if (GridView1.FindControl("tx
Any help is appreciated.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
args.IsValid instead of e.argsValid ;)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I was able to access the properties.
However, how can I call that custom validation method in the RowDataBound method?
protected void GridView1_RowDataBound(obj ect sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
if (GridView1.EditIndex == e.Row.RowIndex)
{
CustomValidator cv1 = new CustomValidator();
cv1.ID = "valxDescription";
cv1.ServerValidate = // I need to call my method ValidateInputLength() here?
cv1.ErrorMessage = "Field length cannot exceed 255 characters.";
cv1.SetFocusOnError = true;
cvl.Display = ValidatorDisplay.Dynamic;
e.Row.Cells[0].Controls.Ad d(cv1);
}
}
}
protected void ValidateInputLength(object source, ServerValidateEventArgs args)
{
TextBox txt = TextBox(GridView1.FindCont rol("txtDe scription" ));
if (txt.Text.Length > 255)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
This is the line where I need to call the custom validation method:
cv1.ServerValidate = // I need to call my method ValidateInputLength() here?
I'm getting overload error or ServerValidate can only be on left side or += or -+ error.
Thanks again for the help. I sincerely appreciate it.
Regards.
However, how can I call that custom validation method in the RowDataBound method?
protected void GridView1_RowDataBound(obj
{
if (e.Row.RowType == DataControlRowType.DataRow
{
if (GridView1.EditIndex == e.Row.RowIndex)
{
CustomValidator cv1 = new CustomValidator();
cv1.ID = "valxDescription";
cv1.ServerValidate = // I need to call my method ValidateInputLength() here?
cv1.ErrorMessage = "Field length cannot exceed 255 characters.";
cv1.SetFocusOnError = true;
cvl.Display = ValidatorDisplay.Dynamic;
e.Row.Cells[0].Controls.Ad
}
}
}
protected void ValidateInputLength(object
{
TextBox txt = TextBox(GridView1.FindCont
if (txt.Text.Length > 255)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
This is the line where I need to call the custom validation method:
cv1.ServerValidate = // I need to call my method ValidateInputLength() here?
I'm getting overload error or ServerValidate can only be on left side or += or -+ error.
Thanks again for the help. I sincerely appreciate it.
Regards.
ServerValidate is an event. you must hook up an event handler for it like so:
cv1.ServerValidate += new ServerValidateEventHandler (ValidateI nputLength );
if you find the event isn't firing correctly, rather create the validator in the itemtemplate and specify the event in the mark-up.
cv1.ServerValidate += new ServerValidateEventHandler
if you find the event isn't firing correctly, rather create the validator in the itemtemplate and specify the event in the mark-up.