We help IT Professionals succeed at work.
Get Started

How to access the  GridView row in CustomValidator event

KavyaVS
KavyaVS asked
on
4,085 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
Commented:
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE