Crumblenautjs Crumblenautjs - 3 months ago 15
ASP.NET (C#) Question

Clear Specific Textboxes ASPX page

I'm having trouble trying to clear these banking and routing numbers that are in a textbox on an aspx page. I've seen it used where they would just specify the ID of the textbox and do a textbox.text = String.Empty(). But that doesn't seem to work here. Maybe I'm using the wrong ID?? I also tried using JQuery .val("") but that didn't seem to work either.

Here's the code, i'd like to clear both Routing and Account text fields on click of a button:

<div id="DivUser1BankInfo" class="labelAndTextboxContainer">
<div class="labelContainer">
<asp:Label CssClass="rightFloat" ID="User1LabelRoutingNumber" runat="server" Text="Routing #:"></asp:Label><br />
</div>
<div class="textboxContainer">
<asp:TextBox ID="User1TextRoutingNumber" CssClass="leftFloat " runat="server" Font-Size="Smaller" Width="180px"
Text='<%# Bind("User1BankRoutingNumber") %>'
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>' /><br />
</div>
<div class="labelContainer">
<asp:Label CssClass="rightFloat" ID="User1LabelAccountNumber" runat="server" Text="Account #:"></asp:Label><br />
</div>
<div class="textboxContainer">
<asp:TextBox ID="User1TextAccountNumber" CssClass="leftFloat " runat="server" Font-Size="Smaller" Width="180px"
Text='<%# Bind("User1BankAccountNumber") %>'
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>' /><br />
</div>
<button type="button" id="clearButton1">Clear</button>

<div class="button">
<asp:Button ID="User1ClearBankInfo" runat="server" Text="Reset"
Visible='<%# ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>' OnClick="clearFields_btn"/><br />
</div>


The OnClick= "clearFields_btn" code behind =

protected void clearFields_btn(object sender, EventArgs e)
{


}


Thanks for any help!

Answer

I haven't worked with ASP.NET in a little while, but I think you may want the OnClientClick event, not OnClick. OnClientClick is for client-side code (your jQuery/JavaScript) and OnClick is for server-side code (your C# or VB.NET).

You'd also want your OnClientClick event method to return false, or the server-side code will also fire.

So I think you want something like:

    <asp:Button ID="User1ClearBankInfo" runat="server" Text="Reset"
    Visible='<%#  ApexRemington.BLL.VendorBLL.ShowUser1BankInfo((string)Eval("User1BankInfoEditUser")) %>
     OnClientClick="clearText();"/>

And then clearText would look like this:

    <script>
    function clearText()
    {

          //our two IDs         

           $('input[id*="User1TextRoutingNumber"]').each(function(index) {
                $(this).val('');
            });
           $('input[id*="User1TextAccountNumber"]').each(function(index) {
                $(this).val('');
            });

           return false;
    }
    </script>

EDIT: shoot, I see my mistake. Fixed the code to clear the text of the textbox, not the button ("this").

Edit: removed the space from the "clear" text val.

EDIT: Made search a little more flexible, less dependent on GridView or no GridView.