0
votes

I have a Gridview in which I want to add a row when the grid is empty using EmptyDataTemplate. The problem is when the grid is empty and I try to add a row, no error occurs but the data row is not added. The Header row is added but not the data. When I put a breakpoint at the start of the RowCommand method, it is not triggered. I can't understand why the event will not trigger. When there is a row present the RowCommand event is triggered and I can add a second row but not an initial row. This is my markup:

<asp:GridView ID="UserGroupGridView" runat="server" AutoGenerateColumns="False" 
    Caption="Group Information" CaptionAlign="Top" CssClass="grid" 
        AllowPaging="true" PageSize="10" 
    HorizontalAlign="Left" ShowHeaderWhenEmpty="True" ShowFooter="true" DataKeyNames="GroupID" 
    onrowcommand="UserGroupGridView_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="GroupID">
            <ItemTemplate>
                <asp:Label ID="uggvLblGroupID" runat="server" Text='<%# Bind("GroupID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Group Name">
            <HeaderTemplate> Group Name
                <asp:ImageButton ID="uggvGroupFilter" runat="server" ImageUrl="Images/filter.png" OnClientClick="return ShowHideFilterTxtBox('uggvTxtNameFilter')" />
                <asp:TextBox ID="uggvTxtNameFilter" runat="server" AutoPostBack="true" style="display:none;" ClientIDMode="Static" OnTextChanged="uggvGridFilter_TextChanged">
                </asp:TextBox>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="uggvLblGroupName" runat="server" Text='<%# Bind("GroupName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="uggvTxtBoxEditGroupName" runat="server" Text='<%# Bind("GroupName") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldEditGroupName" ControlToValidate="uggvTxtBoxEditGroupName" runat="server"
                        ErrorMessage="Required field." ValidationGroup="EditGroupNameValidation" Display="Dynamic" CssClass="message-error">
                </asp:RequiredFieldValidator>            
                <asp:RegularExpressionValidator ID="MaxValEditGroupName" ControlToValidate="uggvTxtBoxEditGroupName" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="EditGroupNameValidation" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                </asp:RegularExpressionValidator>
            </EditItemTemplate>   
            <FooterTemplate>
                <asp:TextBox ID="uggvTxtBoxInsertGroupName" runat="server" Text='<%# Bind("GroupName") %>' ClientIDMode="Predictable"></asp:TextBox>      
                <asp:RequiredFieldValidator ID="RequiredFieldInsertGroupName" ControlToValidate="uggvTxtBoxInsertGroupName" runat="server"
                        ErrorMessage="Required field." ValidationGroup="InsertGroupNameValidation" Display="Dynamic" CssClass="message-error">
                </asp:RequiredFieldValidator>            
                <asp:RegularExpressionValidator ID="MaxValInsertGroupName" ControlToValidate="uggvTxtBoxInsertGroupName" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="InsertGroupNameValidation" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                </asp:RegularExpressionValidator>
             </FooterTemplate>            
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Button ID="uggvEditButton" runat="server" CausesValidation="False" CommandName="Edit" 
                                Text="Edit" CssClass="gridActionbutton">
                </asp:Button>
                &nbsp;<asp:Button ID="uggvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" 
                                Text="Delete" CssClass="gridActionbutton"  OnClientClick="return confirm('Are you sure you want to delete this Group Information?')" >
                </asp:Button>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="uggvUpdateButton" runat="server" CausesValidation="True" ValidationGroup="EditGroupNameValidation" CommandName="Update" 
                                    Text="Update" CssClass="gridActionbutton"></asp:Button>
                &nbsp;<asp:Button ID="uggvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" 
                                    Text="Cancel" CssClass="gridActionbutton"></asp:Button>
            </EditItemTemplate>                  
            <FooterTemplate>
                <asp:Button ID="uggvAddButton" runat="server" CommandName="Add" Text="Add Group" Width="90%" CausesValidation="true" 
                                CssClass="gridActionbutton" ValidationGroup="InsertGroupNameValidation">
                </asp:Button>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        <tr>
            <th>GroupID</th>
            <th>Group Name</th>
            <th>Action</th>
        </tr>
        <tr> 
            <td colspan="3" style="text-align:center;">
                No user-defined groups were found for you. Insert a group name and click the 'Add Group' Button.
            </td> 
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:TextBox ID="uggvTxtBoxInsertGroupName" runat="server" Width="90%"></asp:TextBox>                    
            </td>
            <td>
               <asp:Button ID="uggvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Group" Width="90%" CausesValidation="false" 
                        CssClass="gridActionbutton">
                </asp:Button>
            </td>
        </tr>
     </EmptyDataTemplate>
</asp:GridView>

This is my RowCommand event:

protected void UserGroupGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName.Equals("Add"))
        {
            //Get the Footer controls that have the new entry data
            Control tFooterControls = CommonMethods.getFooterControls(UserGroupGridView);

            string tstrGroupName = (tFooterControls.FindControl("uggvTxtBoxInsertGroupName") as TextBox).Text;
            string tstrLoginUserID = CommonMethods.ParseUserID(User.Identity.Name);

            //Insert into the database
            m_pagingClient.InsertGroup(m_strUserID, tstrGroupName, tstrLoginUserID);

            //Rebind the grid with the new data
            populateGroupGrid();
        }
    }
    catch (Exception ex)
    {
        logger.ErrorException(ex.Message, ex);
       Response.Redirect("~/Error.aspx?use=" + m_strUserType, false);    
    }
}

This grid is the parent grid of a nested grid. But I don't think that should matter.

Can anyone see what I am doing wrong?

Thanks.

UPDATE Below is the validation that I had in the emptytemplatedata control

<EmptyDataTemplate>
            <tr>
                <th></th>
                <th>GroupID</th>
                <th>Group Name</th>
                <th>Action</th>
            </tr>
            <tr> 
                <td colspan="4" style="text-align:center;">
                    No user-defined groups were found for you. Insert a group name and click the 'Add Group' Button.
                </td> 
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td>
                    <asp:TextBox ID="uggvTxtBoxInsertGroupName" runat="server" Width="90%"></asp:TextBox>      
                    <asp:RequiredFieldValidator ID="RequiredFieldInsertGroupNameEmpty" ControlToValidate="uggvTxtBoxInsertGroupName" runat="server"
                            ErrorMessage="Required field." ValidationGroup="InsertGroupNameValidationEmpty" Display="Dynamic" CssClass="message-error">
                    </asp:RequiredFieldValidator>            
                    <asp:RegularExpressionValidator ID="MaxValInsertGroupNameEmpty" ControlToValidate="uggvTxtBoxInsertGroupName" runat="server"
                            ErrorMessage="Maximumn length is 80." ValidationGroup="InsertGroupNameValidationEmpty" Display="Dynamic" CssClass="message-error"
                            ValidationExpression="^.{1,80}$" >
                    </asp:RegularExpressionValidator>                              
                </td>
                <td>                
                    <asp:Button ID="uggvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Group" Width="90%" CausesValidation="true" 
                            CssClass="gridActionbutton" ValidationGroup="InsertGroupNameValidationEmpty">
                    </asp:Button>
                </td>
            </tr>
         </EmptyDataTemplate>

Why would the required validation trigger after I added a value in the text box? UPDATE This is my Page_Load code:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {               
        if (!IsPostBack)
        {
            populateGroupGrid();
            if (m_strUserID.Equals("Global"))
            {
                UserGroupGridView.Caption = "Global Group Information";
            }
            else
            {
                UserGroupGridView.Caption = "User Group Information";
            }
        }
    }
    catch (Exception ex)
    {
        logger.ErrorException(ex.Message, ex);
        Response.Redirect("~/Error.aspx?use=" + m_strUserType, false);
    }
}

This is my grid populate method.

private void populateGroupGrid()
{
    try
    {
        HiddenField hdnFldFilter = (HiddenField)UserGroupGridWrapper.FindControl("uggvHidGridFilter");
        m_strXmlTableData = m_pagingClient.GetGroups(m_strUserID);
        m_dtGroupInfo = CommonMethods.ParseXML(m_strXmlTableData);
        ViewState["GroupInfo"] = m_dtGroupInfo;
        if (!String.IsNullOrEmpty(hdnFldFilter.Value))
        {
            m_dtGroupInfo.DefaultView.RowFilter = hdnFldFilter.Value;
        }
        UserGroupGridView.DataSource = m_dtGroupInfo;
        UserGroupGridView.DataBind();
    }
    catch (Exception ex)
    {
        logger.ErrorException(ex.Message, ex);
       Response.Redirect("~/Error.aspx?use=" + m_strUserType, false);    
    }
}
1
It seems that your RowCommand event doesn't trigger up because of a client side validation...maybe - JCM
Originally, I did have 2 validations for the textbox. A required validation and a RegularExpression validator. When I entered a value in the textbox, the required validation would trigger. I took them out in the above example to try to see why the event would not trigger. I added above the original EmptyTemplateData control. - Gloria Santin
I removed the validation in the gridview and still, it will not call the RowCommand event. - Gloria Santin
Is there a post back at least? - JCM
Yes. There is a post back. I will post the code above. - Gloria Santin

1 Answers

1
votes

I fixed the problem by starting with the basic grid and adding everything back. It is working now but I don't know what in the gridview that was causing the problem. But it all works now.