【问题标题】:Dropdown does not populate when last item deleted from gridview从gridview中删除最后一个项目时不填充下拉菜单
【发布时间】:2015-03-26 13:36:48
【问题描述】:

我无法确定此问题的错误所在。 我有一个具有 Gridview 的 ASP.NET Master.Page Web 应用程序。我正在使用 jquery-chosen 插件作为 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 事件有什么不同?

【问题讨论】:

    标签: c# jquery asp.net gridview


    【解决方案1】:

    问题是我正在检查 FooterControl 是否为空。如果是,我得到了 EmptyTemplateData 控件。删除所有行时,FooterControl 不为空,但需要获取 EmptyTemplateData 控件。 因此,我更改了逻辑以检查 Grid Row Count > 0 而不是空的 FooterControl。 这解决了问题..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多