Solved

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

Posted on 2011-09-09
4
3,594 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 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