首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当删除所有Gridview项目时,不填充jquery选择的下拉列表。

当删除所有Gridview项目时,不填充jquery选择的下拉列表。
EN

Stack Overflow用户
提问于 2015-03-25 15:43:36
回答 1查看 476关注 0票数 0

当用户想要添加到表中时,我在FooterTemplate中的Gridview中有一个jquery选择的下拉框。如果表中没有项,则下拉列表将正确显示用户可以选择的列表。但是,如果用户删除表中的所有项,则EmptyDataTemplate中的下拉列表不会显示任何项。

我相信我必须更新所选的下拉列表,但没有起作用。

这是我的标记:

代码语言:javascript
复制
<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" 
                                        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" 
                                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" 
                                        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="deligvAddButton" runat="server" CommandName="Add" Text="Add Delegate" Width="90%" CausesValidation="false" 
                        CssClass="gridActionbutton">
                    </asp:Button>
                </td>
            </tr>
        </EmptyDataTemplate>
     </asp:GridView>

这是在页脚中填充下拉列表的代码隐藏:

代码语言:javascript
复制
 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");
        }
    }

这是我的javascript:

代码语言:javascript
复制
 <script type="text/javascript">
    $(document).ready(function () {
        //Configure the DropDownBox using the 'chosen' jquery plugin
        $(".chosen-single").chosen({
            search_contains: true,
            no_results_text: "Sorry, no match!"                           
        });
    });

    $('#deligvAddButtonEmpty').click(function () {
        $(".chosen-single").chosen().val('').trigger("chosen:updated");
    });

</script>

因此,当单击按钮deligvAddButtonEmpty时,项目不会添加到下拉列表中。我遍历了代码,在返回数据时调用了方法DelegateInfoGridView_DataBound。但是下拉列表是空的。

我相信我的javascript是不正确的,但我不知道如何修复它。如有任何建议,将不胜感激。

谢谢。

更新

我更改了按钮上的javascript,单击如下:

代码语言:javascript
复制
 $('#deligvAddButtonEmpty').click(function () {
        $("#deligvDDLRecipientName").trigger("chosen:updated");
    });

但是当网格为空时,下拉列表仍然是空的。

更新

我正在尝试使用Firebug调试javascript,而且似乎没有调用.click函数。至少,当我在函数中放置一个断点时,它不会像在document.ready函数中那样停止。我使用的是MasterPages,属性为ClientIDMode =‘ClientIDMode’,但可能没有正确定义‘选择器’。

更新

不是在点击'Add Button‘时,而是当'Delete’按钮被单击并且网格中没有更多的项时,选择的下拉列表就不会刷新。但是,我似乎无法让单击事件触发按钮。这是我的jquery单击函数:

代码语言:javascript
复制
 $(document).on("click", "input[id$=deligvDeleteButton]", function () {
        alert("Delegate delete button clicked");
    });

至少更新,现在正在触发单击函数.我将ClientIDMode-“静态”添加到正在访问的控件中,并将'click‘函数放在document.ready()函数中。但是,更新没有将列表添加到下拉列表中。这是一项功能:

代码语言:javascript
复制
$("input[id$=deligvDeleteButton]").click(function () {
              $("input[id$=deligvDDLRecipientName]").val('').trigger("chosen:updated");
            });

更新

我不知道这是否是问题所在,但这可能与jquery脚本中的控制ID有关,无论是在GridView中还是在EmptyDataTemplate中。对于GridView或EmptyDataTemplate中的下拉列表,我将I保持不变。在代码隐藏中,我通过确定FooterControl是否为null来识别所使用的控件。那么C#代码的其余部分是相同的,因为is是相同的。但是,在javascript中,如果ids相同,这可能是一个问题。

如何识别EmptyDataTemplate中的控件而不是Gridview中的控件?

更新

我不知道这是否是问题所在,但这可能与jquery脚本中的控制ID有关,无论是在GridView中还是在EmptyDataTemplate中。对于GridView或EmptyDataTemplate中的下拉列表,我将I保持不变。在代码隐藏中,我通过确定FooterControl是否为null来识别所使用的控件。那么C#代码的其余部分是相同的,因为is是相同的。但是,在javascript中,如果ids相同,这可能是一个问题。

如何识别EmptyDataTemplate中的控件而不是Gridview中的控件?

我使用以下javascript获取2个下拉控件的I,并更新这两个控件,而下拉列表仍未填充:

代码语言:javascript
复制
 $("input[id$=deligvDeleteButton]").click(function () {               
            $("[id*='deligvDDLRecipientName']").each(function () {
                alert($(this).attr('id'));
                $(this).val("").trigger("chosen:updated");
            });            
        });

警报显示ID,一个是'deligvDDLRecipientName‘,另一个是deligvDDLRecipientName_chosen。我用.trigger函数更新它们,但是列表仍然没有填充。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-27 17:56:04

我想出来了..。万一有人有这个问题..。

这与选择的插件无关。如果FooterControl被实例化的话,这就是我识别的方式。我查过零了。但是,当用户删除所有行时,FooterControl不是null,但仍然需要使用EmptyTemplateData控件。因此,我没有检查null,而是检查行计数> 0。起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29260242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档