[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to access the  GridView row in CustomValidator event

Posted on 2012-08-14
5
Medium Priority
?
3,673 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
0
Comment
Question by:KavyaVS
  • 3
  • 2
5 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 600 total points
ID: 38293677
Try replacing

custval.NamingContainer

with

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

Either one should work.
0
 

Author Comment

by:KavyaVS
ID: 38296327
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
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38297252
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.
0
 

Accepted Solution

by:
KavyaVS earned 0 total points
ID: 38309535
Now I am able to access the GridViewRow using this line.
GridViewRow gvr = custval.NamingContainer as GridViewRow;

Thanks.
0
 

Author Closing Comment

by:KavyaVS
ID: 38333794
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question