[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-09-09
4
Medium Priority
?
3,712 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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