【问题标题】:Identifying EmptyDataTemplate control in Jquery在 Jquery 中识别 EmptyDataTemplate 控件
【发布时间】:2015-03-26 05:30:08
【问题描述】:

我需要在 jquery 函数调用中标识我在 GridView 的 EmptyDataTemplate 中拥有的控件。 在代码隐藏中,您可以这样做来获取所有控件:

 Control tFooterControls = grid.Controls[0].Controls[0];

然后是 FindControl("") 以获取特定控件。 jquery中是否有等价物?

我无法使用 ID,因为我为 Gridview 控件使用与 EmptyDataTemplate 相同的 ID,以简化代码隐藏。

这是我的 GridView 标记:

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

更新

这是一个代码隐藏,一旦它确定了它来自的行,它就会为 Gridview 或 EmptyDataTemplate 使用相同的 ID:

protected void DelegateInfoGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("Add"))
            {
                //Get the Footer Controls that have the data
                Control tFooterControls = getFooterControls(DelegateInfoGridView);
                //Get the data for each control           
                int tiDelegateID = Convert.ToInt32((tFooterControls.FindControl("deligvDDLRecipientName") as DropDownList).SelectedValue);                 
                bool tbIsActive = convertIsActiveValue((tFooterControls.FindControl("deligvDDLActiveInsert") as DropDownList).SelectedValue);
                m_strUserID = CommonMethods.ParseUserID(User.Identity.Name);

                //Insert into database
                m_pagingClient.InsertDelegateInformation(m_strUserID, tiDelegateID, tbIsActive);

                //Refresh the grid
                populateDelegateInfoGrid();
            }
        }
        catch (Exception ex)
        {
            //TO DO: Response.Redirect("~/Error.aspx");
        }
    }

更新

我能够使用下面的 jquery 获取元素的 ID。但是,它仍然没有解决我更新下拉列表的问题。

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

【问题讨论】:

  • 它们是不同类型的元素吗? $("table[id='elementID']")您可以按标签名称和ID选择。
  • 我认为问题在于 Gridview 中的 FooterControl 与 EmptyDataTemplate 控件具有相同的 ID。我这样做是因为当我确定“添加”命令是来自 EmptyDataTemplate 还是来自 FooterControl 时,我只有元素的“FindControl”。我会把代码放在上面。 “表”会识别 EmptyDataTemplate 吗?
  • 我尝试了您的建议,但仍然无法识别 EmptyDatatemplate 中的 DropDown 列表。

标签: javascript jquery asp.net


【解决方案1】:

我能够使用下面的 jquery 获取元素的 ID。但是,它仍然没有解决我更新下拉列表的问题。

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

【讨论】:

    猜你喜欢
    • 2010-10-11
    • 2018-01-12
    • 2013-07-08
    • 2017-02-02
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多