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

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

setting focus to a textbox

Posted By:      Posted Date: October 22, 2010    Points: 0   Category :ASP.Net

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


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

page.setfocus(listbox.selecteditem) is not setting the focus if index > 820

Hi All, Does any one know the reason Why I am unable to setfocus to my list box if the index > 820 My list box contains 2000 items and the index is not  able to set the focus if the index > 820  

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"

Setting value of asp:textbox in Javascript in ASP.net 3.5


I created a page with several controls (ddl, tbx) that I use for filtering a Gridview.  I created a javascript function that runs on the OnClientClick Event of an <asp:button  to set the value of an <asp:TextBox to today and a Selected Value in a ddl. 

function setMyToday() {
            var tbxToday = document.getElementById('ctl00_cntPage_tbxToday');
            var tbxUID = document.getElementById('ctl00_cntPage_tbxUserID');
            var ddlPV = document.getElementById('ctl00_cntPage_ddlProvider');
            tbxToday.value = makeToday();
            ddlPV.value = tbxUID.value;

The button submits the page and returns some records in a Gridview.  I can see the value in the tbxToday when the button is clicked and the row in the drop down list, but when the page is posted,the value in the text bos is lost and as a result is not used to filter the records.  However the value in the ddlProvider is

Setting textbox value inside modal popup


Question from a newbie:

In the problem code, there is a modal popup that is shown using .Show, and that part works fine.  A textbox in the popup needs to display text, but I can't get that part to work.

testModalPopUp = Page.FindControl("ExampleModalPopUp")
testTextBox = Page.FindControl("ExampleTextBox")

testTextBox.Text = "something goes here"

Is there something I'm missing?


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. 

Setting a focus on a datagridview - visual basic 2010


i am was able to cause the mouse wheel to work on my datagridview.

now i have noticed that when the form first loads i actually need to click the datagridview to set the focus on it and then i can scroll

i added two line for my form load event

    ' Selecting first datagrid row
        grdAccountTypes.Rows(0).Selected = True

but that didn't solve my problem either.


any ideas ?


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 on cell in datagrid after CollectionChanged event.




Having trouble setting the focus on a cell after the collection recieves a new row. This is my code:

 sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
			DataGridRowObjects<My_DataGridRow> oc = (DataGridRowObjects<My_DataGridRow>)sender;
 (e.Action == NotifyCollectionChangedAction.Add)
				//set focus to column 2 of the new row

				DataGridCell cell = my_datagrid.GetCell(e.NewStartingIndex, 2);
 (cell != null
				 DataGridCellInfo info = new
				 my_datagrid.CurrentCell = info;

Is there any event that fires after the CollectionChanged? My DataGridRow doesn't have a CellPresenter att this stage and GetCell fails. If I do the same with a button event instead, it works fine.

I just tried to add a dispatcher message instead but that did nothing:

Dispatcher.BeginInvoke(new Action(delegate()
  //set focus to column 2 of the new row
  DataGridCell c

Setting textbox background-color changes the default border



I've got a textbox that I'm temporarily applying a class to to give it white text on a white background.  For some reason when I apply that class, the border style changes from the default single, gray, flat border to an inset, 2px, gray border. I messed around with different combinations of styles and colors and it seems as though setting any background-color on the textbox causes the border to change. Where does the default border style come from and why is it changing?

Set Focus on Textbox


    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.


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



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