locked
Validation Problem for gridview nested in repeater RRS feed

  • Question

  • User646364117 posted

    I have a problem that when a validation error occurs from a required field validator it happens in all of the nested gridviews, not just the offending one. 

    After doing some research I found an example which showed how to assign validation groups dynamically.

    I put the following code in the gridview OnDataBpund event but it fails because it can't find the first requiredfieldvalidator i.e., RequiredFieldValidator3

    Maybe I need to choose a different event?

    Protected Sub gvContent_OnDataBound(sender As Object, e As EventArgs)
            Dim gv As GridView = CType(sender, GridView)
            Dim rowIndexForFooter As Integer = gv.Rows.Count - 1
    
            For Each row As GridViewRow In gv.Rows
                If row.RowType = DataControlRowType.DataRow Then
                    Dim RequiredFieldValidator3 As RequiredFieldValidator = CType(row.Cells(1).FindControl("RequiredFieldValidator3"), RequiredFieldValidator)
                    Dim RequiredFieldValidator5 As RequiredFieldValidator = CType(row.Cells(2).FindControl("RequiredFieldValidator5"), RequiredFieldValidator)
                    RequiredFieldValidator3.ValidationGroup = "gv_update" & rowIndexForFooter.ToString()
                    RequiredFieldValidator5.ValidationGroup = "gv_update" & rowIndexForFooter.ToString()
                    Dim Button_Update As ImageButton = CType(row.Cells(0).FindControl("btn_Update"), ImageButton)
                    Button_Update.ValidationGroup = "gv_update" & rowIndexForFooter.ToString()
                End If
                If row.RowType = DataControlRowType.Footer Then
                    Dim RequiredFieldValidator4 As RequiredFieldValidator = CType(gv.FooterRow.Cells(1).FindControl("RequiredFieldValidator4"), RequiredFieldValidator)
                    Dim RequiredFieldValidator6 As RequiredFieldValidator = CType(gv.FooterRow.Cells(2).FindControl("RequiredFieldValidator6"), RequiredFieldValidator)
                    RequiredFieldValidator4.ValidationGroup = "gv_insert" & rowIndexForFooter.ToString()
                    RequiredFieldValidator6.ValidationGroup = "gv_insert" & rowIndexForFooter.ToString()
                    Dim Button_Insert As ImageButton = CType(gv.FooterRow.Cells(0).FindControl("btn_Insert"), ImageButton)
                    Button_Insert.ValidationGroup = "gv_insert" & rowIndexForFooter.ToString()
                End If
    
            Next
    
        End Sub

    Here is my markup for the gridview
    <asp:GridView ID="gvContent"
                                                    AutoGenerateColumns="false"
                                                    runat="server"
                                                    OnRowCommand="gvContent_RowCommand" OnDataBound="gvContent_OnDataBound"   ShowFooter="True">
    
                                                    <Columns>
                                                        <asp:TemplateField HeaderStyle-Width="100px">
                                                            <ItemTemplate>
                                                                <asp:ImageButton ID="btn_Edit" runat="server" CommandName="EditRow" CommandArgument='<%#Eval("contentid") %>' src="images/edit_20.png" CausesValidation="False"  />
    
                                                                <asp:ImageButton ID="btn_Delete" runat="server" CommandName="DeleteRow" CommandArgument='<%#Eval("contentid") %>' src="images/delete_20.png" CausesValidation="False"
                                                                    OnClientClick="return confirm('Are you sure you want to delete this row?')" />
                                                            </ItemTemplate>
                                                            <EditItemTemplate>
                                                                <asp:ImageButton ID="btn_Update" runat="server" CommandName="UpdateRow" CommandArgument='<%#Eval("contentid") %>' src="images/save_20.png"  CausesValidation="true" />
    
                                                                <asp:ImageButton ID="btn_Cancel" runat="server" CommandName="CancelUpdate" CommandArgument='<%#Eval("contentid") %>' src="images/cancel_20.png" CausesValidation="False" />
    
                                                            </EditItemTemplate>
                                                            <FooterTemplate>
                                                                <asp:ImageButton ID="btn_Insert" runat="server" CommandName="InsertRow"
                                                                    src="images/addnew_20.png" CausesValidation="True"
                                                                     />
                                                                <asp:ImageButton ID="btn_CancelInsert" runat="server" CommandName="CancelInsert" CommandArgument='<%#Eval("contentid") %>' src="images/cancel_20.png" CausesValidation="False" />
                                                            </FooterTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Short Text" HeaderStyle-Width="300px">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lbl_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <EditItemTemplate>
                                                                <asp:TextBox ID="txt_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:TextBox>
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Short Text Required"
                                                                    ControlToValidate="txt_shorttext" />
    
    
                                                            </EditItemTemplate>
                                                            <FooterTemplate>
                                                                <asp:TextBox ID="txt_insert_shorttext" runat="server" Text="" />
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Short Text Required" 
                                                                    ControlToValidate="txt_insert_shorttext" />
    
                                                            </FooterTemplate>
                                                        </asp:TemplateField>
    
                                                        <asp:TemplateField HeaderText="Long Text" HeaderStyle-Width="400px">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lbl_longtext" runat="server" Text='<%#Eval("longtext") %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <EditItemTemplate>
                                                                <asp:TextBox ID="txt_longtext" runat="server" TextMode="MultiLine" Text='<%#Eval("longtext") %>' />
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Long Text Required" 
                                                                    ControlToValidate="txt_longtext" />
                                                            </EditItemTemplate>
                                                            <FooterTemplate>
                                                                <asp:TextBox ID="txt_insert_longtext" runat="server" TextMode="MultiLine" Text=""></asp:TextBox>
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Long Text Required" 
                                                                    ControlToValidate="txt_insert_longtext" />
    
                                                            </FooterTemplate>
                                                        </asp:TemplateField>
    
    
                                                        <asp:TemplateField HeaderText=" Content ID" Visible="false">
                                                            <ItemTemplate>
                                                                <asp:TextBox ID="txt_id" runat="server" Value='<%#Eval("contentid") %>'></asp:TextBox>
                                                            </ItemTemplate>
                                                     
                                                        </asp:TemplateField>
                                                    </Columns>
                                                </asp:GridView>

    Friday, February 14, 2020 3:09 PM

All replies

  • User-1330468790 posted

    Hi,  sg48asp,

    I understand that you want to validate the row which is in line with the button (update/insert) without affecting other lines.

    The events to find the control:

    The way you took is incorrect since you cannot fetch the validator directly from GridView and the validators are contained in each row. So, you have to find the validator in Row event, e.g. RowDataBound.

    For Validators in footer row,  as I suggested in another case you posted, you can find and assign value to the controls in footer after binding data to the gridview.

    About "rowIndexForFooter":

    This value should not treated as an unique value to separate different rows. Otherwise, each row in gridview will be put in the same validation group.

    I suggest you use an unique ID (key in database) for each line while I constructed pseudo-unique values for the below demo.

    More details, you can refer to below code which is in line with that I posted in previous problems.

    .aspx Page

    <asp:Repeater ID="rptBlock" runat="server" OnItemDataBound="rptBlock_ItemDataBound">
    
                    <ItemTemplate>
    
                        <asp:Label ID="Label1" runat="server" Text="Block: " />
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("blockid") %>' />
                        <asp:GridView ID="gvContent"
                            AutoGenerateColumns="false"
                            runat="server"
                            OnRowEditing="gvContent_RowEditing"
                            OnRowCancelingEdit="gvContent_RowCancelingEdit"
                            OnRowCommand="gvContent_RowCommand" OnDataBound="gvContent_DataBound"
                            OnRowDataBound="gvContent_RowDataBound"
                            ShowFooter="True">
    
                            <Columns>
                                <asp:TemplateField HeaderStyle-Width="100px">
                                    <ItemTemplate>
                                        <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%#Eval("contentid") %>' CausesValidation="False" />
    
                                        <asp:Button ID="btn_Delete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Eval("contentid") %>' CausesValidation="False"
                                            OnClientClick="return confirm('Are you sure you want to delete this row?')" />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update" CommandArgument='<%#Eval("contentid") %>' CausesValidation="true" />
    
                                        <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" CommandArgument='<%#Eval("contentid") %>' CausesValidation="False" />
    
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:HiddenField ID="hf_blockid" runat="server" />
                                        <asp:Button ID="btn_Insert" runat="server" Text="Insert" CommandName="InsertRow"
                                             CausesValidation="True" />
                                        <asp:Button ID="btn_CancelInsert" runat="server" Text="Cancel Insert" CommandName="CancelInsert" CommandArgument='<%#Eval("contentid") %>' CausesValidation="False" />
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Short Text" HeaderStyle-Width="300px">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_shorttext" runat="server" Text='<%#Eval("shorttext") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Short Text Required"
                                            ControlToValidate="txt_shorttext" />
    
    
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txt_insert_shorttext" runat="server" Text="" />
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Short Text Required"
                                            ControlToValidate="txt_insert_shorttext"  />
    
                                    </FooterTemplate>
                                </asp:TemplateField>
    
                                <asp:TemplateField HeaderText="Long Text" HeaderStyle-Width="400px">
                                    <ItemTemplate>
                                        <asp:Label ID="lbl_longtext" runat="server" Text='<%#Eval("longtext") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txt_longtext" runat="server" TextMode="MultiLine" Text='<%#Eval("longtext") %>' />
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Long Text Required"
                                            ControlToValidate="txt_longtext" />
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txt_insert_longtext" runat="server" TextMode="MultiLine" Text=""></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Long Text Required"
                                            ControlToValidate="txt_insert_longtext" />
    
                                    </FooterTemplate>
                                </asp:TemplateField>
    
    
                                <asp:TemplateField HeaderText=" Content ID" Visible="false">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txt_id" runat="server" Value='<%#Eval("contentid") %>'></asp:TextBox>
                                    </ItemTemplate>
    
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                       
                    </ItemTemplate>
                    <SeparatorTemplate>
                        <br />
                        <hr />
                        <br />
                    </SeparatorTemplate>
                </asp:Repeater>

    Code-Behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
            If Not IsPostBack Then
                BindRepeater()
            End If
        End Sub
    
        Private Sub BindRepeater()
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("blockid", GetType(Integer))
    
            For i As Integer = 0 To 3 - 1
                dt.Rows.Add(i)
            Next
    
            rptBlock.DataSource = dt
            rptBlock.DataBind()
        End Sub
    
        Private Sub BindGridViewByBlockID(ByVal blockid As Integer, ByVal gd As GridView)
            Dim dt As DataTable = New DataTable()
            dt.Columns.Add("id", GetType(Integer))
            dt.Columns.Add("contentid", GetType(Integer))
            dt.Columns.Add("shorttext", GetType(String))
            dt.Columns.Add("longtext", GetType(String))
    
            For i As Integer = 0 To 4 - 1
                dt.Rows.Add(i, (blockid * 4) + i, "shortext-" & i & "(blockid-" & blockid & ")", "longtext-" & i & "(blockid-" & blockid & ")")
            Next
    
            gd.DataSource = dt
            gd.DataBind()
            Dim hf As HiddenField = CType(gd.FooterRow.FindControl("hf_blockid"), HiddenField)
            hf.Value = blockid.ToString()
            Dim Button_Insert As Button = CType(gd.FooterRow.Cells(0).FindControl("btn_Insert"), Button)
            Button_Insert.CommandArgument = blockid.ToString()
            Dim RequiredFieldValidator4 As RequiredFieldValidator = CType(gd.FooterRow.Cells(1).FindControl("RequiredFieldValidator4"), RequiredFieldValidator)
            Dim RequiredFieldValidator6 As RequiredFieldValidator = CType(gd.FooterRow.Cells(2).FindControl("RequiredFieldValidator6"), RequiredFieldValidator)
            RequiredFieldValidator4.ValidationGroup = "gv_insert" & blockid.ToString()
            RequiredFieldValidator6.ValidationGroup = "gv_insert" & blockid.ToString()
            Button_Insert.ValidationGroup = "gv_insert" & blockid.ToString()
        End Sub
    
        Protected Sub rptBlock_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim gv As GridView = CType(e.Item.FindControl("gvContent"), GridView)
                Dim lbl As Label = CType(e.Item.FindControl("Label2"), Label)
                Dim blockid As Integer = Convert.ToInt32(lbl.Text)
                BindGridViewByBlockID(blockid, gv)
            End If
        End Sub
    
        Protected Sub gvContent_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        End Sub
    
        Protected Sub gvContent_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        End Sub
    
        Protected Sub gvContent_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            gd.EditIndex = e.NewEditIndex
            Dim row As GridViewRow = gd.Rows(e.NewEditIndex)
            Dim hf As HiddenField = CType(gd.FooterRow.FindControl("hf_blockid"), HiddenField)
            Dim blockid As Integer = Convert.ToInt32(hf.Value)
            BindGridViewByBlockID(blockid, gd)
        End Sub
    
        Protected Sub gvContent_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
            Dim gd As GridView = CType(sender, GridView)
            gd.EditIndex = -1
            BindRepeater()
        End Sub
    
        Protected Sub gvContent_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            Dim gv As GridView = CType(sender, GridView)
            Dim row As GridViewRow = e.Row
    
            If row.RowType = DataControlRowType.DataRow AndAlso (row.RowState And DataControlRowState.Edit) > 0 Then
                Dim RequiredFieldValidator3 As RequiredFieldValidator = CType(row.Cells(1).FindControl("RequiredFieldValidator3"), RequiredFieldValidator)
                Dim RequiredFieldValidator5 As RequiredFieldValidator = CType(row.Cells(2).FindControl("RequiredFieldValidator5"), RequiredFieldValidator)
                Dim Button_Update As Button = CType(row.Cells(0).FindControl("btn_Update"), Button)
                Dim contentid As String = Button_Update.CommandArgument
                RequiredFieldValidator3.ValidationGroup = "gv_update" & contentid.ToString()
                RequiredFieldValidator5.ValidationGroup = "gv_update" & contentid.ToString()
                Button_Update.ValidationGroup = "gv_update" & contentid.ToString()
            End If
        End Sub

    Demo:

    Hope this can help you.

    Best regards,

    Sean

    Monday, February 17, 2020 11:46 AM