none
Clear Textbox field after clicking button RRS feed

  • Question

  • I have created a gridview that is pulling data from a sql table. When I enter text to search for in Textbox1 and click on Button2, I get the gridview to display with the search return.  I am trying to clear the textbox once I get the return from the sql table.  Anyone know how to do this?
    Saturday, May 25, 2019 7:47 PM

All replies

  • I'm not sure you can do that.  If you clear the textbox the gridview will probably respond as if there were no text entered.

    I would usually handle that by having an instruction for the textbox that said something like "enter your search term here:"

    Then it is obvious to your user that they would need to change the search text for a different result.


    ClarkNK AKA HomePage Doctor
    Database Tutorials Using ASP.NET Controls
    My recommended web hosting company: RDOServers

    Saturday, May 25, 2019 9:18 PM
  • You can clear the textbox after the search is run and the gridview is bound to that data.  The gridview won't respond as if there was no text entered unless you re-do the search (clicking Button2 again).

    EW doesn't have the concept of a code-behind page, and this is not something you can do without knowing how to code one yourself, or, alternatively, using the single page model - putting the code in a script section in the page itself.

    Assuming your GridView has id GridView1, you could add the following script in the .aspx page itself, after the Page declaration at the top.  (This assumes the page language is C#.):

    <script runat="server">
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            Textbox1.Text = "";
        }
    </script>

    Then, be sure to add the following attribute to your GridView declaration (inside your <asp:GridView ...   /> line):

     OnDataBound="GridView1_DataBound"


    Kathleen Wilber
    BrightWillow - Asp.Net Applications






    • Edited by KathyW2 Sunday, May 26, 2019 5:05 AM
    Sunday, May 26, 2019 4:18 AM
  • Hmm -- before I wrote my response I had tried similar (see below), which caused the gridview to show no results in response to clearing the textbox:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
                TxtBxLName.Text = "Enter last name here"
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            GridView1.DataBind()
    End Sub

    Protected Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound

            TxtBxLName.Text = ""
    End Sub


    ClarkNK AKA HomePage Doctor
    Database Tutorials Using ASP.NET Controls
    My recommended web hosting company: RDOServers

    Monday, May 27, 2019 1:59 AM
  • I don't know your SQLDataSource markup, but if you have the select parameter set as a control parameter, with the textbox1 as the control, decouple it.  Make it a simple parameter.

    As an example:

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [UserName], [Id] FROM [AspNetUsers] WHERE [Id] = @Id">
            <SelectParameters>
                <asp:Parameter Name="Id" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

    Then in the button click event, set the value of the select parameter to the textbox1 text.  This is now a copy of that text, and doesn't depend on future values of TextBox1, until you click the button again.

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters["Id"].DefaultValue = TextBox1.Text;
            GridView1.DataBind();
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            TextBox1.Text = "";
        }


    Kathleen Wilber
    BrightWillow - Asp.Net Applications

    Monday, May 27, 2019 12:09 PM