如何删除和撤消使用javascript删除gridview行?

时间:2012-05-04 03:59:49

标签: javascript jquery asp.net gridview

我有一个gridview,点击一个删除按钮,我需要删除该行一段时间,然后单击撤消链接,我需要重新显示该行。

我的gridview代码如下所示:

  <asp:GridView ID="NotificationHistory" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" DataKeyNames="Id" 
        ShowHeader="False" PagerSettings-Mode="NumericFirstLast"
        FooterStyle-BorderStyle="None" EnableViewState="False" GridLines="None" >
        <PagerSettings Mode="NumericFirstLast"></PagerSettings>
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <img alt="Delete" title="Delete" src="SiteImages/X.gif" 
                        onclick="changeImage(this)" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ItemName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ItemName") %>'></asp:Label>
                </ItemTemplate>

            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle BorderColor="#F1E8E8" BorderStyle="Solid" BorderWidth="1px" CssClass="gridItemAlignment" />
            </asp:TemplateField>
            <asp:BoundField DataField="Id" HeaderText="ID" Visible="false" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="ImgDelete" runat="server" Width="16px" Height="16px" CommandName="Remove"
                        ImageUrl="SiteImages/X.gif" ClientIDMode="Static" Enabled="false" />
                    <asp:HiddenField ID="hFType" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle Height="10px" BorderColor="Silver" BorderStyle="None" BorderWidth="0" />
        <FooterStyle BorderStyle="None"></FooterStyle>
        <PagerStyle HorizontalAlign="Right" BorderStyle="None" ForeColor="#999999"></PagerStyle>
        <EmptyDataTemplate>
            <asp:Label ID="lblHistory" runat="server">No History.</asp:Label>
        </EmptyDataTemplate>
    </asp:GridView>

我的HTML:

<table id="ctl00_Content_NotificationHistory" class="home-history-grid" cellspacing="0" border="0" style="border-color: rgb(241, 232, 232); border-style: none; width: 100%; border-collapse: collapse;">
<tbody>
<tr class="home-history-grid-row">
<td>
<img height="16px" width="16px" onclick="changeImage(this)" src="SiteImages/X.gif" title="Delete" alt="Delete">
</td>
<td class="gridItemStyle" style="border: 1px solid rgb(241, 232, 232); width: 75%;">
<a href="ReportCreation.aspx?rid=207">Status Report for Kalista as on 05/03/2012</a>
</td>
<td class="gridItemAlignment" style="border: 1px solid rgb(241, 232, 232);">Saved</td>
<td>
<img id="ImgDelete" height="16px" width="16px" onclick="deleteRow(this)" src="SiteImages/X.gif" alt="">
<input id="ctl00_Content_NotificationHistory_ctl02_hFType" type="hidden" value="Cirrus.CirrusObjects.Report" name="ctl00$Content$NotificationHistory$ctl02$hFType">
</td>
</tr>
<tr class="home-history-grid-row">
<td>
</td>
<td class="gridItemStyle" style="border: 1px solid rgb(241, 232, 232); width: 75%;">
</td>
<td class="gridItemAlignment" style="border: 1px solid rgb(241, 232, 232);">Saved</td>
<td>
</td>
</tr>
</tbody>
</table>

我的javascript删除行(这是删除选定的行,现在我想知道如何在同一个地方重新显示它):

 function deleteRow(imageElement) {
            var img = $(imageElement);
            imageElement.setAttribute("id", "selectedRow");
            alert($("selectedRow").siblings(":image"));
            var row = $(imageElement).closest('tr');
            $(row).remove();
        }

2 个答案:

答案 0 :(得分:2)

这将显示/隐藏行

$(row).toggle();

答案 1 :(得分:1)

将此$(行).remove()更改为

$(row).hide();它会被隐藏,当你想再次展示它时$(row).show();

或者甚至会更加光滑

$(row).toggle();(在场景后面设置display ='none'隐藏)