Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

some_textbox.focus()....without postback

Status
Not open for further replies.

domeika

Technical User
May 15, 2002
3
US
[hammer]
I'm trying to set the focus to a textbox control on a page. All controls are web (server-side)controls. I have a button that when pressed, the onclick event takes the values from 2 textboxes, calls a concatenation function which combines the 2 values into one string and places it in a listbox. This works just fine but after the value is placed in the listbox, I need the focus to go to the first textbox again. This all MUST be done before the page postback.
I tried 'RegisterClientScriptBlock(yada,yada,yada)' where it would use javascript to set the focus on the control but javascript errors and says null or not an object which means it can't find it. For some reason web controls don't seem to visible to it.
Any ideas?
 
You can try this:

private void Page_Load(object sender, System.EventArgs e)
{
if(myButton.Attributes["onclick"] == null)
{
myButton.Attributes.Add("onclick", "javascript:document.myForm.myTextBox.focus()");
}
}

This will add a client-side event handler to your button which will be executed before the post back. Id you have ViewState enabled, then the text box should keep focus after the page's reloaded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top