We help IT Professionals succeed at work.

How to access the  GridView row in CustomValidator event

KavyaVS
KavyaVS asked
on
Medium Priority
4,039 Views
Last Modified: 2012-08-26
Hi,
I am trying to access the GridView row(Updating row) in the custom validator server validate event.

I am getting this error: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.GridViewRow'..aspx page

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

            <Columns>

                <asp:BoundField DataField="ID" HeaderText="ID" />
                 <asp:CommandField ShowEditButton="True"  ButtonType="Link"        CausesValidation="True">
                                                    </asp:CommandField>
                <asp:TemplateField HeaderText="Validation Test">

                    <ItemTemplate>
                     <asp:Label ID="Name" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>    
                      <asp:TextBox ID="TextBox1" runat="server"
                           Text='<%# Bind("Name")%>'></asp:TextBox>
                         <asp:CustomValidator ID="CustomValidator1" runat="server"

                            ErrorMessage="CustomValidator" Text="*"

                            onservervalidate="CustomValidator1_ServerValidate"

                            ValidateEmptyText="True"></asp:CustomValidator>

                    </EditItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>


Code behind:

public void CustomValidator1_ServerValidate(object sender, System.Web.UI.WebControls.ServerValidateEventArgs args)
        {  
              String nameRegex = "^[-a-zA-Z0-9-,. ]{1,40}";
             CustomValidator custval = new CustomValidator();
            custval = (CustomValidator)sender;
               GridViewRow gvr = (GridViewRow)custval.NamingContainer;
             try
            {
                switch (custval.ID.ToString())
                {
                         case "CustomValidator1":
                        {
                            TextBox txtNm = (TextBox)gvr.FindControl("TextBox1");
                            if (txtNm.Text.Trim().Equals(string.Empty))
                            {
                                args.IsValid = false;
                            }
                            else if (!Regex.IsMatch(txtNm.Text.Trim(), nameRegex))
                            {
                                args.IsValid = false;
                            }
                            else
                            {
                                args.IsValid = true;

                            }
                            break;
                        }
                   
                }
                catch (Exception ex)
                {
               
                }
                finally
                {


                }

        }

Please let me know how to access the GridViewRow.

Thanks
Comment
Watch Question

Obadiah ChristopherDeveloper User Interface
CERTIFIED EXPERT
Commented:
Try replacing

custval.NamingContainer

with

custval.Parent.Parent;
or
custval.Parent.Parent.Parent;

Either one should work.

Author

Commented:
I tried both. But nothing worked out.I got the following error.
Unable to cast object of type 'System.Web.UI.WebControls.Table' to type 'System.Web.UI.WebControls.GridViewRow'.

I went further and added custval.Parent.Parent.Parent.parent; But no luck.

Any help please.

Thanks
Obadiah ChristopherDeveloper User Interface
CERTIFIED EXPERT

Commented:
Can u place a debugger at the custVal.Parent and see what are you getting as the parent. Can't really help without seeing your actual data.
Commented:
Now I am able to access the GridViewRow using this line.
GridViewRow gvr = custval.NamingContainer as GridViewRow;

Thanks.

Author

Commented:
Thanks