asp.netonclickaspx-user-control

C# code to clear textbox on click is not working, error


I have created a aspx webpage that searches a sql database and that search is returned in a gridview. I am using a textbox and button to do the search. The search works fine but I am trying to clear the textbox on button click.

My cs coding is

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnCallWeb.Sql_Searches
{
    public partial class Plate_Lookup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButSearch_Click(object sender, EventArgs e)
        {
            TextBox1.Text = string.Empty;
        }

        protected void ButMaster_Click(object sender, EventArgs e)
        {
            TextBox2.Text = string.Empty;
        }
    }
}```

and my aspx code is 
```                    <span class="auto-style16">&nbsp;&nbsp;</span><asp:Button ID="ButSearch" runat="server" Font-Bold="True" Font-Size="Large" Text="Search" Width="100px" CssClass="auto-style16" OnClick="ButSearch_Click" />
&nbsp;&nbsp; <span class="auto-style16">Enter Master Incident ID:
                    &nbsp;<asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style16"></asp:TextBox>
&nbsp;</span><asp:Button ID="ButMaster" runat="server" Font-Bold="True" Font-Size="Large" Text="Search" Width="100px" />
                <p class="auto-style19" style="text-align: center; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-indent: 0px; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
                    &nbsp;<p class="auto-style19" style="text-align: center; font-variant-ligatures: normal; font-variant-caps: normal; orphans: 2; text-indent: 0px; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
                    <strong><span class="auto-style20">Plate Info:</span></strong><div class="auto-style15">
                        <asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" EmptyDataText="No Records Found!" AutoGenerateColumns="False" CssClass="auto-style16" DataSourceID="CADArchive" HorizontalAlign="Center" Font-Size="Medium">
                            <Columns>
                                <asp:BoundField DataField="Master_Incident_ID" HeaderText="Master Incident ID" SortExpression="Master_Incident_ID" />
                                <asp:BoundField DataField="PlateNumber" HeaderText="PlateNumber" SortExpression="PlateNumber" />
                                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                                <asp:BoundField DataField="EntryDate" HeaderText="EntryDate" SortExpression="EntryDate" />
                                <asp:BoundField DataField="UserInitials" HeaderText="UserInitials" SortExpression="UserInitials" />
                            </Columns>

                </asp:GridView>```

When I run the application I am getting an error on the line for <span class="auto-style16">&nbsp;</span><asp:Button ID="ButSearch" runat="server" Font-Bold"True" Font-Size="Large" Text="Search" Width="100px" CssClass="auto-style16" OnClick="ButSearch_Click" />```

I have tried TextBox1.Text = String.Emtpy; and TextBox1.text=""; and neither work.

Solution

  • Looking at the markup, I don't see a Textbox1, but only a TextBox2?

    Either your posted markup is wrong, or missing Textbox2.

    With your given markup, then try

    TextBox2.Text = "";