我在一个面板中有16个文本框。我正在尝试创建一个按钮,清除每个框中的所有文本。目前我正在使用txtbox.text = ""
选项,但必须有更好的方法来实现它。
我尝试过使用panel.controls.clear()
,但这似乎也不起作用。
<asp:Panel ID="pnlAddNewPeopleRecord" runat="server" Visible="false">
<asp:Label ID="lblAddNewPeopleRecord" runat="server" Visible="true">Add To People Table</asp:Label>
<asp:Table runat="server" class="tblAddNewRecord">
<asp:TableRow>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">ID</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeoplePersonID"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">First Name</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleFirstName"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Middle Initial</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleMiddleInitial"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Last Name</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleLastName"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Street 1</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleStreet1"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Street 2</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleStreet2"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">City</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleCity"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">State</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleState"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Zip Code</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleZipCode"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Country</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleCountry"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Phone 1</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeoplePhone1"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Phone 2</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeoplePhone2"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Fax</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleFax"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Cell Phone</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleCellPhone"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Email</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleEmail"></asp:TextBox>
</asp:TableCell>
<asp:TableCell CssClass="tblCell">
<asp:Label class="tblCellLabel" runat="server">Notes</asp:Label>
<asp:TextBox class="tblCellTextbox" runat="server" ID="txtbxPeopleNotes"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button runat="server" class="btnResetRecord" ID="btnAddNewPeopleResetRecords" Text="Reset Fields" OnClick="btnAddNewPeopleResetRecords_Click" />
<asp:Button runat="server" class="btnAddNewRecord" ID="btnAddNewRecordToPeople" Text="Add Record To People Table" OnClick="btnAddNewRecordToPeople_Click" />
</asp:Panel>
我正在使用下面的代码。当我单击按钮时,页面什么都不做。
protected void btnAddNewPeopleResetRecords_Click(object sender, EventArgs e)
{
resetAllTextBoxes(pnlAddNewPeopleRecord);
}
private void resetAllTextBoxes(System.Web.UI.WebControls.Panel pnlAddNewPeopleRecord)
{
foreach(System.Web.UI.Control c in pnlAddNewPeopleRecord.Controls)
{
if (c is System.Web.UI.WebControls.TextBox)
{
((System.Web.UI.WebControls.TextBox)c).Text = "";
}
}
}
答案 0 :(得分:0)
如果您想在服务器端执行此操作,那么解决方案是最好的方法。如果linq可供您使用,它可能会被加强。
using System.Web.UI.WebControls;
private void resetAllTextBoxes(Panel pnlAddNewPeopleRecord)
{
foreach(var control in pnlAddNewPeopleRecord.Controls.OfType<TextBox>())
{
control.Text = string.Empty;
}
}
或者,如果您不需要回发到服务器,则可以执行此客户端。使用jquery轻松。
<input type="button" value="Clear Textboxes" onclick="ClearTextboxes();" />
<script type="text/javascript">
function ClearTextboxes()
{
$(".tblCellTextbox").val("");
}
</script>