.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Set Focus on Textbox

Posted By:      Posted Date: October 22, 2010    Points: 0   Category :WPF

    I have a composite control with Listbox and a Textbox. I am trying to achieve a functionality where in if I type something in the text box, the list box items gets filtered based on the text typed.

   I am able to get this working, however I am having issues in retaining the Keyboard focus in the Textbox.

   I am doing this functionality on the Key Up Event of the Textbox. If I type a text the event handler is getting triggered and performing the operation, but the focus goes from the Textbox

I am forcing the Focus by calling the Focus() function on the Textbox, but it doesn't seem to be working.

 SDK of Focus function says: "Even if an element is focusable and enabled, event handling within a specific tree, (such as for a composite control) might respond to the preview focus events by not allowing focus there, thus this method would return false."

How can I force the focus on the Textbox object.


View Complete Post

More Related Resource Links

How come I can't set focus to a textbox in DetailsView?

Here's the first part of a DetailsView control definition:          <asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Edit" AutoGenerateRows="False" Width="100%" BorderColor="Silver">            <RowStyle Height="25px" />            <Fields>              <asp:TemplateField HeaderText="Name" SortExpression="Name">                <EditItemTemplate>                  <asp:TextBox ID="textBoxName" runat="server" Text='<%# Bind("Name") %>' CssClass="detailsTextBox" onclick="SelectText2(this)" onKeyUp="EnableSubmit()" />                </EditItemTemplate>                <InsertItemTemplate>        &nb

why isnt the focus being set to a textbox in the gridview?

Hi, i have a gridview with textboxes in. a barcode reader is used to enter data in the textboxes in the grid. when something is scanned in the page does a full postback. so after a postback and after something has been scanned in, i want the focus to be moved to the next textbox in the row inside the gridview.  i have some code, but it doesnt seem to work. after the postback the focus is set to one of the textboxes outside the grid. i need it to recognise one of the textboxes in the grid. i have tried to use the findcontrol property to do this still doesnt work. please can anyone see where i am going wrong. what i wanted to do is for it to automatically tab to the next textbs in the gridview after a postback. please any advise on this would be much appreciated. thank you very muchprotected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as Page); int indx = wcICausedPostBack.TabIndex; var ctrl = from control in wcICausedPostBack.Parent.Controls.OfType<WebControl>() where control.TabIndex > indx select control; ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus(); } } protected Control GetControlThatCausedPostBack(Page page)

textbox focus problem


I have a textbox (txtMaiden) and a radio button list (rblGender) in two separate User Controls that I'm using for a registration site. The txtMaiden control appears or disappears depending on whether Male or Female is designated on the radio button. This all works fine but I want the textbox to have focus when it becomes visible. As of now, it only gets focus when the page refreshes. Code follows. Any ideas?

<Prf:gender ID="GenderE" Gender='<%#Bind("Gender")%>' runat="server" />
<Prf:maiden ID="MaidenE" Maiden='<%#Bind("MaidenName")%>' Maiden_Disp='<%#Bind("MaidenName_Disp")%>'
    runat="server" />

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="gender.ascx.vb" Inherits="userControls_profile_gender" %>
<asp:Panel ID="pnlGender" Visible="true" runat="server">
    <div class="label">
        <asp:Label ID="lblGender" Text="Sex:" runat="server" />
    <div class="ctrl">
                <asp:RadioButtonList ID="rblGender" AppendDataBoundItems="True" AutoPostBack="True"

keep focus on textbox on textchange event



How can I keep focus on caller textbox on textchange event or set focus on a given textbox according to a given value?

How can I know which textbox triggered the event if I need to use the same textchange method?

thanks in advance

Silverlight - Textbox on focus wait till a Asynchronous process is complete



I have a textbox, where i call its lostfocus event when the value changes.

In lost focus i do a Asynchronous process to update a grid with the text entered. 

I have a scenario in which immediately after changing the value in textbox i hit a button, though the button click event raises i want it to wait till the asysnchronous process in lost focus of the textbox is complete. i want to do this as i use the result from lostfocus in button click event.

Please let mt know if there is a way to make the lost focus wait till it completes the asynchronous call.



codebehind set focus textbox field in ajaxtoolkit : Tab


Hi all ,

I am stuck in the set focus field in field in ajaxtoolkit : Tab  at codebehind when button click.

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    If StringUtil.IsStringEmpty(Me.ddlSex.SelectedValue) = True Then

       Page.ClientScript.RegisterStartupScript(Me.GetType, "ErrMsg", 
            JScriptUtil.ErrorMsg("Jantina (Sex): "

    End if

   End Sub

How to prevent Loss of Focus if there is Validation error on a textBox?

Hi i have a TextBox displayed as a TreeViewItem and when the user enters text and Tabs away or clicks somewhere else i want the control to retain focus if there is any validation error.Is there anyway i can do that? I am adding the ValidationRule to the TextBox created dynamically in code..I do see the default redborder when there is a validation error.I just want to force the user to correct the error and not go anywhere else in the app until its fixed..
Thanks in advance...

MaskedEditExtender, text removed if you lose focus and set focus back on textbox


I have a MaskedEditExtender with Mask="(999) 999-9999".

Now my problem is, if someone goes into the textbox and enters


Then they leave the textbox, and then see that they forgot to enter the area code, if they go back in there to enter the area code, it starts overwritting the other numbers instead of just adding to them. 

How to set focus to UserName textbox in Login Control ?



can you please tell how to focus cursor to UserName TextBox in Login control when page is loaded for the first time in browser ? 

setting focus to a textbox


using vb.net/asp.net 2005

after the user clicks on a button in the buttonClick event I am trying to set the focus at the end of the function simply as follows:


However the focus is instead going to another button.  The tabIndex property is not set on any controls on the page.  Has anyone experience this?  thanks


How to determine which textbox has focus?



I am trying to set the color of a textbox that has focus to gray. How can I tell which textbox currently has the curser on it so when they click the change color button it will change the textbox that is focused. Something like this:

focusedtextbox.BackColor = Drawing.Color.LightGray



set focus textbox inside modalpopupextender


hi all.

i am working wit modal popup extender where i wan to set focus to a text box in popup.

can any 1 help me out with detail. plz 

abour changing border of textbox when focus textbox


Hi.i try to do this: when mouse come over the textbox, border color will change. When the mouse leave over the textbox, border of text turn the old type. Also, when textbox is focused,  border of textbox color will change. my problem here is focus event. i did mouseover and mouseleave. but i can't do focus event. My codes is below:

$("#txtsearch").mouseover(function ()


How to set a Label style when the target TextBox has focus?

I would like to highlight a label when the associated textbox has focus. This works:

<Label Grid.Row="1" Grid.Column="0" Target="{Binding ElementName=CountryCode}">
    <Style TargetType="{x:Type Label}">
          <DataTrigger Binding="{Binding ElementName=CountryCode, Path=(IsFocused)}" Value="True">
              <Setter Property="Foreground" Value="Blue" />
   <AccessText Text="{Binding Path=CountryCodeLabel}" />
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryCode}" />

But I have a bunch of these textboxes, so I'd prefer to template the style. This works:

<Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">

How to set focus on next textbox on Enter Key using jQuery

Normally, tab key is used to move to next text box and by default, when Enter key is pressed, the form gets submitted or it calls the default button click event. But it would be a nice feature for end-user to give him ability to move to next textbox using Enter key.

Textbox Editable GridView in ASP.NET

This article describes how I made a fully editable gridview in C# using ASP.NET 2. The idea is that the GridView looks and works like an Excel spreadsheet. You see all the cells in the table, and you can edit any of the cells you like, and they are automatically updated (i.e. saved to the database). This is very often how people expect data tables to work in web pages, and I've often seen people clicking in vain on un-editable gridview cells, somehow expecting to edit them, and not understanding that they need to click an Edit button at the end of the row.

Highlighting Rows with TextBox OnFocus

In another article I explained that how you can make the rows of the GridView control clickable and highlight them when they are clicked. In this article I will explain that how you can highlight the GridView rows when you focus on the TextBox which is contained inside the GridView control.
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend