从gridview删除最后一项时,不会填充下拉列表

时间:2015-03-26 13:36:48

标签: c# jquery asp.net gridview

我无法确定此问题的错误位置。 我有一个具有Gridview的ASP.NET Master.Page Web应用程序。我使用jquery选择的插件作为EmptyDataTemplate中的下拉列表(如果有数据,则使用FooterControl)。

在初始化时,如果网格没有数据,则会填充下拉列表并正确显示。如果网格中有项目并且我删除了所有项目,那么没有数据,下拉列表不会显示任何数据。调用DataBound事件,DataTable中包含所有正确的数据。它必然会下拉。但是项目会出现在列表中。 这是我的标记:

<div id="DelegateGridWrapper">
    <asp:GridView ID="DelegateInfoGridView" runat="server" 
        AutoGenerateColumns="false" Caption="Delegate Information" 
        CaptionAlign="Top" CssClass="grid" RowStyle-Wrap="true" 
        HorizontalAlign="Left" ShowFooter="true" 
        AllowPaging="true" PageSize="5"  ShowHeaderWhenEmpty="false" onrowediting="DelegateInfoGridView_RowEditing" 
        onrowcancelingedit="DelegateInfoGridView_RowCancelingEdit" onrowdeleting="DelegateInfoGridView_RowDeleting" 
        onrowupdating="DelegateInfoGridView_RowUpdating" 
        ondatabound="DelegateInfoGridView_DataBound" 
        onrowcommand="DelegateInfoGridView_RowCommand">
        <Columns>
             <asp:TemplateField HeaderText="Recipient ID">
                <ItemTemplate>
                    <asp:Label ID="deligvLblRecipientID" runat="server" Text='<%# Bind("RecipientID") %>'></asp:Label>
                </ItemTemplate>
             </asp:TemplateField>

             <asp:TemplateField HeaderText="Delegate" ItemStyle-Wrap="false"> 
                <ItemTemplate>
                    <asp:Label ID="deligvLblRecipientName" runat="server" Text='<%# Bind("RecipientName") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="deligvDDLRecipientName" runat="server" ClientIDMode="Static"
                                        data-placeholder="Choose delegate…" class="chosen-single">
                    </asp:DropDownList>
                </FooterTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Active"> 
                <ItemTemplate>
                    <asp:Label ID="deligvLblActive" runat="server" Text='<%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %>'></asp:Label>
                </ItemTemplate>         
                 <EditItemTemplate>
                    <asp:DropDownList ID="deligvDDLActive" runat="server" Text='<%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %>'>
                        <asp:ListItem>Yes</asp:ListItem>
                        <asp:ListItem>No</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate> 
                <FooterTemplate>
                    <asp:DropDownList ID="deligvDDLActiveInsert" runat="server">
                        <asp:ListItem Selected="True">Yes</asp:ListItem>
                        <asp:ListItem>No</asp:ListItem>
                    </asp:DropDownList>
                </FooterTemplate>                         
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
                 <ItemTemplate>
                    <asp:Button ID="deligvEditButton" runat="server" CausesValidation="False" CommandName="Edit" 
                                Text="Edit" CssClass="gridActionbutton">
                    </asp:Button>
                    &nbsp;<asp:Button ID="deligvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" ClientIDMode="Static" 
                                Text="Delete" CssClass="gridActionbutton"  OnClientClick="return confirm('Are you sure you want to delete this Delegate Information?')" >
                    </asp:Button>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Button ID="deligvUpdateButton" runat="server" CausesValidation="False" CommandName="Update" 
                                    Text="Update" CssClass="gridActionbutton"></asp:Button>
                    &nbsp;<asp:Button ID="deligvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" 
                                    Text="Cancel" CssClass="gridActionbutton"></asp:Button>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Button ID="deligvAddButton" runat="server" CommandName="Add" Text="Add Delegate" Width="90%" CausesValidation="false" 
                                CssClass="gridActionbutton">
                    </asp:Button>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            <tr>
                <th>Recipient ID</th>
                <th>Delegate</th>
                <th>Active</th>
                <th>Action</th>
            </tr>
            <tr> 
                <td colspan="4" style="text-align:center;">
                    No Delegates were found for you. Delegates can be added by clicking the 'Add Delegate' Button.
                </td> 
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:DropDownList ID="deligvDDLRecipientName" runat="server" ClientIDMode="Static" 
                                        data-placeholder="Choose delegate…" class="chosen-single">
                    </asp:DropDownList>
                </td>
                <td>
                     <asp:DropDownList ID="deligvDDLActiveInsert" runat="server">
                        <asp:ListItem Selected="True">Yes</asp:ListItem>
                        <asp:ListItem>No</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:Button ID="deligvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Delegate" Width="90%" CausesValidation="false" 
                        CssClass="gridActionbutton">
                    </asp:Button>
                </td>
            </tr>
        </EmptyDataTemplate>
     </asp:GridView>

这是我的DataBound事件:

 protected void DelegateInfoGridView_DataBound(object sender, EventArgs e)
    {
        try
        {

            m_strUserID = CommonMethods.ParseUserID(User.Identity.Name);

            //Get the Footer controls that have the new entry data
            Control tFooterControls = getFooterControls(DelegateInfoGridView);
            DropDownList ddlRecipientNames = tFooterControls.FindControl("deligvDDLRecipientName") as DropDownList;              
            m_strXmlTableData = m_pagingClient.GetAllPossibleDelegates(m_strUserID);
            DataTable tdtAllDelegates = CommonMethods.ParseXML(m_strXmlTableData);
            ddlRecipientNames.DataSource = tdtAllDelegates;
            ddlRecipientNames.DataTextField = "RecipientName";
            ddlRecipientNames.DataValueField = "RecipientID";
            ddlRecipientNames.DataBind();
            ddlRecipientNames.Items.Insert(0, new ListItem("", "0"));//This is needed for the jquery-chosen dropdown to add data-holder text
        }
        catch (Exception ex)
        {
            //TO DO: Response.Redirect("~/Error.aspx");
        }
    }

为什么下拉列表会在删除所有项目后显示项目,但如果最初在gridview中没有要显示的项目,则会正确显示? 我尝试触发所选下拉列表的更新,但最初在检索数据之前调用。 我不知道代码隐藏中是否存在错误,或者我是否需要在javascript中添加内容。

感谢。

更新

问题不在于所选插件。我从DropDown列表中删除了属性,将其更改为“选定”样式,问题仍然存在。因此,在用户删除网格中的所有项目后,不会填充asp:DropDownList。但是如果网格初始化时没有任何项目,则DropDown会正确填充。

更新 我有一个建议是使用'RowDeleted'事件来绑定下拉列表。但是,事件没有解雇。我将事件添加到标记中:

onrowdeleted="DelegateInfoGridView_RowDeleted"

这是永远不会被调用的事件:

 protected void DelegateInfoGridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        try
        {
            m_strUserID = CommonMethods.ParseUserID(User.Identity.Name);

            //Get the Footer controls that have the new entry data
            Control tFooterControls = getFooterControls(DelegateInfoGridView);
            DropDownList tddlRecipientNames = tFooterControls.FindControl("deligvDDLRecipientName") as DropDownList;
            m_strXmlTableData = m_pagingClient.GetAllPossibleDelegates(m_strUserID);
            DataTable tdtAllDelegates = CommonMethods.ParseXML(m_strXmlTableData);
            tddlRecipientNames.DataSource = tdtAllDelegates;
            tddlRecipientNames.DataTextField = "RecipientName";
            tddlRecipientNames.DataValueField = "RecipientID";
            tddlRecipientNames.DataBind();
            tddlRecipientNames.Items.Insert(0, new ListItem("", "0"));//This is needed for the jquery-chosen dropdown to add data-holder text
        }
        catch (Exception ex)
        {
            //TO DO: Response.Redirect("~/Error.aspx");
        }
    }

它不会触发的RowDeleted事件有什么不同?

1 个答案:

答案 0 :(得分:0)

问题是我正在检查FooterControl是否为空。如果是的话,我得到了EmptyTemplateData控件。删除所有行时,FooterControl不为null,但需要获取EmptyTemplateData控件。 所以,我改变逻辑以检查网格行计数&gt; 0而不是null FooterControl。 这解决了问题..