如何在 JavaScript 中删除 GridView 中的所有行?
我有一个 GridView 和一个 TextBox。如果我们在 TextBox 中输入文本,我想清除 GridView 中的所有行。如何使用 JavaScript 来做到这一点?
在页面加载中,我从后端填充网格。它可能有行,也可能没有。但我想在 TextBox 中输入文本时清除 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>
I have One GridView and One TextBox. If We enter the text in the TextBox, I want to clear all the Rows in the GridView. How to do this using JavaScript?
In the Page Load I fill the Grid from the BackEnd. It may have rows or not. But I want to clear the GridView when I enter the text in the TextBox.
Here is my Source Code.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个。
代码隐藏
JavaScript
Try this.
Code-behind
JavaScript
沿着这些思路的东西应该可以解决问题。
我假设你正在使用 jQuery。我们需要访问 ASP.NET 为 HTML 节点生成的客户端 ID。
something along these lines should do the trick.
I'm assuming you are using jQuery. We need access to the client side ID that ASP.NET generates for the HTML nodes.
将网格包裹在
DIV
上并捕获文本框的onchange
事件。在文本onchange
事件中清除DIV 的innerHTML
。Wrap the grid on a
DIV
and captureonchange
event of the textbox. Clear theinnerHTML
of the DIV in the textonchange
event.