Solved

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

Posted on 2011-09-09
4
3,559 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
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
Gewgala earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks so much!
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
Glad I could help ;)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now