Solved

Asp.net C#  onkeyup & checking at each keypress for a textbox

Posted on 2011-09-09
4
3,634 Views
Last Modified: 2013-11-08
I want to filter the display rows in a combobox as someone types into a textbox, so for evey letter typed the combobox is more filtered..

I got all to look & work fine except for the fact that at each charator check the textbox looses focus. When I reset the focus it goes to the front of the word, not the back..  So to type in Bruce you would need to have to type in ecurB..  Or mouse click or arrow to the back of the word entry.

There must be a fix for this!

Below is the code. The Session["EmpName" is tied to a SQLDataSource that uses it for a filter.. This is tied to the listbox.
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetupTable();
                Session["EmpName"] = "%";
                sdsEmpName.DataBind();
                lbEmpNames.DataBind();
                txtLastName.Attributes.Add("onkeyup",
                    Page.ClientScript.GetPostBackEventReference(txtLastName, ""));
                txtFirstName.Attributes.Add("onkeyup",
                   Page.ClientScript.GetPostBackEventReference(txtFirstName, ""));
            }
        }

        protected void txtLastName_TextChanged(object sender, EventArgs e)
        {
            Session["EmpName"] = txtLastName.Text.Trim() +"%" + ", " + txtFirstName.Text.Trim() + "%";
            sdsEmpName.DataBind();
            lbEmpNames.DataBind();
            if (lbEmpNames.Items.Count == 1)
            {
                lbEmpNames.Items[0].Selected = true;
                SelectedEmp(lbEmpNames.SelectedValue);
            }
            txtLastName.Focus();

        }

        protected void txtFirstName_TextChanged(object sender, EventArgs e)
        {
            Session["EmpName"] = txtLastName.Text.Trim() + "%" + ", " + txtFirstName.Text.Trim() + "%";
            sdsEmpName.DataBind();
            lbEmpNames.DataBind();

            if (lbEmpNames.Items.Count == 1)
            {
                lbEmpNames.Items[0].Selected = true;
                SelectedEmp(lbEmpNames.SelectedValue);
            }

            txtFirstName.Focus();

        }

Open in new window

0
Comment
Question by:moosetracker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
Gewgala earned 500 total points
ID: 36512855
It might seem pretty simple, but try this.  

Put the following in your textbox declaration:

<asp:Textbox onfocus="this.value=this.value;" />

I've had to use a similar tactic in the past to put the character at the end of the input text rather then beginning when the textbox obtains focus.  What the above javascript does is just say "when the textbox gets focus, assign the current text value to it's value property" which sets the cursor at the end.

Give that a try...
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36512941
And, if that doesn't work, do try this out:

<asp:Textbox onfocus="focusText(this);" />

then have a JavaScript function defined as such:

function focusText(obj)
{
     var pos = obj.value.length;
     if(obj.setSelectionRange)
    {
        obj.setSelectionRange(pos,pos);
    }
    else if (obj.createTextRange) {
        var range = obj.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

Open in new window


This example should explicitly say "move the cursor to the end of the textbox value when the textbox gets focus"
0
 
LVL 1

Author Closing Comment

by:moosetracker
ID: 36514805
Thanks so much!
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36514826
Glad I could help ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question