2016-12-14 11 views
0

パネル内に16個のテキストボックスがあります。私は各ボックスのすべてのテキストをクリアするボタンを作成しようとしています。現在、私はtxtbox.text = ""オプションを使用していますが、より良い方法が必要です。パネル内のテーブル内のすべてのテキストボックスをクリアするC#

私は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 = ""; 
     } 
    } 
} 
+1

このWebフォームは、私はwebformsにあまり慣れていないが、私の最初の提案は、値をクリアするためにjavascript(特にjQuery)を利用することであり、それは完全にクライアント側で行うことができます。それがオプションでない場合は、WebFormsでAjaxを調べて、ボタンが完全なポストバックなしでサーバーに呼び出されるようにする必要があるかもしれません。これはMVCの基本的なアイデアです。 –

答えて

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> 
+0

Javascriptを追加すると、フィールドがクリアされません。私も最初の提案を試みましたが、それはどちらもうまくいきませんでした – kevorski