如何在JavaScript中从GridView中删除所有行?

如何在JavaScript中从GridView中删除所有行?,javascript,asp.net,user-controls,Javascript,Asp.net,User Controls,我有一个GridView和一个文本框。如果我们在文本框中输入文本,我想清除GridView中的所有行。如何使用JavaScript实现这一点 在页面加载中,我从后端填充网格。它可能有行,也可能没有行。但我想在文本框中输入文本时清除GridView 这是我的源代码 <table> <tr> <td> <asp:TextBox ID="txtPercentage" runat="server"><

我有一个GridView和一个文本框。如果我们在文本框中输入文本,我想清除GridView中的所有行。如何使用JavaScript实现这一点

在页面加载中,我从后端填充网格。它可能有行,也可能没有行。但我想在文本框中输入文本时清除GridView

这是我的源代码

 <table>
    <tr>
        <td>
            <asp:TextBox ID="txtPercentage" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
     <td>
        <asp:GridView ID="gvAttribute" runat="server" AutoGenerateColumns="False" CssClass="datagrid"
            EnableTheming="False" DataKeyNames="TaxAttributeID" OnRowDataBound="gvAttribute_RowDataBound"
            OnSelectedIndexChanged="gvAttribute_SelectedIndexChanged" Width="100%">
            <EmptyDataTemplate>
                <table width="100%" border="0" cellpadding="0" cellspacing="1">
                    <tr>
                        <th scope="col">
                            Attribute Name
                        </th>
                        <th scope="col">
                            Tax Percentage
                        </th>
                        <th scope="col">
                            Reference Amount
                        </th>
                        <th scope="col">
                            Reference Amount
                        </th>
                        <th scope="col">
                            Sign
                        </th>
                        <th scope="col">
                            Tax Amount
                        </th>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <AlternatingRowStyle CssClass="table_oddrow" />
            <Columns>
                <asp:BoundField DataField="TaxAttributeID" HeaderText="TaxAttributeID" Visible="False" />
                <asp:BoundField DataField="TaxAttributeName" HeaderText="Attribute Name" />
                <asp:BoundField DataField="TaxPercentage" HeaderText="Tax Percentage" />
                <asp:BoundField DataField="ReferenceAmount" HeaderText="Reference Amount" />
                <asp:BoundField DataField="ReferenceTypeID" HeaderText="ReferenceTypeID " Visible="False" />
                <asp:BoundField DataField="ReferenceType" HeaderText="Reference Type" />
                <asp:TemplateField HeaderText="CrDrIndicator" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="lblCrDrIndicator" runat="server" Text='<%# Bind("CrDrIndicator") %>'></asp:Label></ItemTemplate>
                </asp:TemplateField>
                <%--<asp:BoundField DataField="CrDrIndicatorName" HeaderText="Sign" />--%>
                <asp:TemplateField HeaderText="Sign">
                    <ItemTemplate>
                        <asp:TextBox ID="txtgvSign" runat="server" Text='<%# Bind("CrDrIndicatorName") %>'
                            Style="background-color: Transparent; border: 0px;">
                        </asp:TextBox>
                    </ItemTemplate>
                    <ControlStyle Width="50px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Tax Amount">
                    <ItemTemplate>
                        <asp:TextBox ID="txtgvTaxAmount" runat="server" Text='<%# Bind("TaxAmount") %>' Style="background-color: Transparent;
                            border: 0px;"></asp:TextBox></ItemTemplate>
                    <ControlStyle Width="75px" />
                </asp:TemplateField>
                <asp:BoundField DataField="DiscountText" HeaderText="Discount Text" Visible="False" />
                <asp:TemplateField HeaderText="Formula Text" Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="lblFormulaText" runat="server" Text='<%# Bind("FormulaText") %>'></asp:Label></ItemTemplate>
                </asp:TemplateField>
                <%--<asp:BoundField DataField="TaxAmount" HeaderText="Tax Amount" />--%>
                <asp:TemplateField Visible="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkSelect" runat="server" CommandName="select" Text="select" /></ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle ForeColor="White" />
            <PagerStyle CssClass="pager_style" />
            <RowStyle CssClass="table_evenrow" />
            <SelectedRowStyle CssClass="rowselect" />
        </asp:GridView>
      </td>
  </tr>
</table>

属性名
税率
参考金额
参考金额
签名
税额

按照这些思路做的事情应该可以奏效

<script>
  $('#<%# txtPercentage.ID %>').bind("change", function(){
    $('#<%# gvAttribute.ID %> table tbody tr').remove();
  });
</script>

$('#').bind(“change”,function(){
$('#table tbody tr')。删除();
});

我假设您正在使用jQuery。我们需要访问ASP.NET为HTML节点生成的客户端ID。

按照这些思路应该可以做到这一点

<script>
  $('#<%# txtPercentage.ID %>').bind("change", function(){
    $('#<%# gvAttribute.ID %> table tbody tr').remove();
  });
</script>

$('#').bind(“change”,function(){
$('#table tbody tr')。删除();
});

我假设您正在使用jQuery。我们需要访问ASP.NET为HTML节点生成的客户端ID。

将网格包装在
DIV
上,并捕获文本框的
onchange
事件。清除文本
onchange
事件中DIV的
innerHTML

将网格包装在
DIV
上,并捕获文本框的
onchange
事件。清除文本
onchange
事件中DIV的
innerHTML

试试这个

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        string script = String.Format("javascript:return DeleteGridView('{0}');", gvAttribute.ClientID);
        txtPercentage.Attributes.Add("onkeydown", script);
    }
}
JavaScript

function DeleteGridView(grid_id) {
    var grid = document.getElementById(grid_id);
    if (grid.parentNode) {
        grid.parentNode.removeChild(elem);
    }
    return false;
}
试试这个

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        string script = String.Format("javascript:return DeleteGridView('{0}');", gvAttribute.ClientID);
        txtPercentage.Attributes.Add("onkeydown", script);
    }
}
JavaScript

function DeleteGridView(grid_id) {
    var grid = document.getElementById(grid_id);
    if (grid.parentNode) {
        grid.parentNode.removeChild(elem);
    }
    return false;
}

张贴您的标记并作进一步解释。@naveen:我编辑了我的问题,请检查。@thevan:清除gridview意味着删除行。空文本框还是什么?@naveen。删除GridView中的所有行。是否删除整个GridView本身?你想保留标题吗?发布你的标记并解释多一点。@naveen:我编辑了我的问题,请检查。@thevan:清除gridview意味着删除行。空文本框还是什么?@naveen。删除GridView中的所有行。是否删除整个GridView本身?你想保留标题吗?@Naffas:No.Iam使用JavaScript。请用JavaScript写出来。@Naffas:不,我是用JavaScript写的。请用JavaScript给出它。