无法使用emptydatatemplate向gridview添加行

时间:2016-02-15 21:47:03

标签: asp.net gridview

我有一个Gridview,我想在网格为空时使用EmptyDataTemplate添加一行。问题是当网格为空并且我尝试添加行时,不会发生错误但不添加数据行。添加了标题行,但未添加数据。当我在RowCommand方法的开头放置一个断点时,它不会被触发。我无法理解为什么事件不会触发。当存在一行时,会触发RowCommand事件,我可以添加第二行但不能添加初始行。 这是我的标记:

<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>

这是我的RowCommand事件:

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);    
    }
}

此网格是嵌套网格的父网格。但我认为这不重要。

谁能看到我做错了什么?

感谢。

更新 以下是我在emptytemplatedata控件中的验证

<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>

为什么在文本框中添加值后会触发所需的验证? 的更新 这是我的Page_Load代码:

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);
    }
}

这是我的网格填充方法。

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 个答案:

答案 0 :(得分:1)

我通过从基本网格开始并添加所有内容来解决问题。它现在正在工作,但我不知道gridview中导致问题的原因。但现在一切正常。