Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

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("txtDescription").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("txtDescription").Text.Length > 255)

Any help is appreciated.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Velio
Velio
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
args.IsValid instead of e.argsValid ;)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of -Dman100-

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(object 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.Add(cv1);

            }
        }
    }

    protected void ValidateInputLength(object source, ServerValidateEventArgs args)
    {
        TextBox txt = TextBox(GridView1.FindControl("txtDescription"));

        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(ValidateInputLength);

if you find the event isn't firing correctly, rather create the validator in the itemtemplate and specify the event in the mark-up.