1

I am working on a C# ASP.NET 4 project where I need to have a search box that dynamically searches the content from database's table and shows it in Repeater Control everytime I enter alphabet in TextBox.

I have done it upto this but the problem is

1) I am losing focus on TextBox everytime I enter single alphabet
2) When I erase content in TextBox, Repeater Control still shows data

<asp:TextBox ID="TextBox1" runat="server" Width="90%" 
                                ontextchanged="TextBox1_TextChanged" 
                                onKeyUp="return serachme()" 
                                AutoPostBack="false"></asp:TextBox>
                                <script language="javascript" type="text/javascript">
                                    function serachme() {
                                        __doPostBack('<%=TextBox1.UniqueID %>', "onKeyUp");
                                    }
                                </script>

And,

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    con.Open();
    SqlDataAdapter mycommand2 = new SqlDataAdapter("select qid,title from globalq where title like '%" + TextBox1.Text + "%'", con);
    DataSet ds = new DataSet();
    mycommand2.Fill(ds);
    askQ.DataSource = ds;
    askQ.DataBind();
    con.Close();
}

I already saw examples of Set focus in TextBox after postback and Set focus in TextBox after postback but did not understood it coz I dont know JavaScript that much.

So my Question is

1) How to get focus on TextBoX everytime I __doPostBack?
2) When I erase content in TextBox, Why Repeater Control still shows data and how to not show that?

Thanks for your efforts in advance,

Nikhil

Community
  • 1
  • 1
Nikhil Tamhankar
  • 439
  • 5
  • 12
  • 30

1 Answers1

1

Have you tried this?:

con.Open(); 
SqlDataAdapter mycommand2 = new SqlDataAdapter("select qid,title from globalq where title like '%" + TextBox1.Text + "%'", con); 
DataSet ds = new DataSet(); 
mycommand2.Fill(ds); 
askQ.DataSource = ds; 
askQ.DataBind(); 
con.Close(); 

//apply focus back to textbox
TextBox1.Focus();
James Johnson
  • 45,496
  • 8
  • 73
  • 110