Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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