在Jquery中识别EmptyDataTemplate控件

时间:2015-03-25 20:37:47

标签: javascript jquery asp.net

我需要在jquery函数调用中识别GridView的EmptyDataTemplate中的控件。 在代码隐藏中,您可以执行此操作以获取所有控件:

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

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

我无法使用ID,因为我使用与EmptyviewTemplate相同的Gridview控件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");
            });            
        });

1 个答案:

答案 0 :(得分:0)

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

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