Auto Focus the Element (Button Textbox etc)

hi friends,

some times you need to set auto focus to certain elements and usage of Default button in asp.net panel may not work. JavaScript set focus may not help you  so in that situation please find the appropriate solution as given below

for example you have several asp.net buttons in a page but you want to set focus on only particular button named Button1

Step 1)  in aspx page the button to which you want to set focus might be like this

<asp:Button ID="Button1 " Text="Next" runat="server" />

Step 2) write a small javascript function like this

function SetBtnNextFocus()
{
if(document.getElementById('ctl00_MainContentPlaceHolder_Button1')!=null)
{
setTimeout(function() {   document.getElementById('ctl00_MainContentPlaceHolder_Button1').focus(); }, 1000);
}
}

Step 3) goto Code behind side and write this part of code in Page_PreRender like this

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "jsService123", "SetBtnNextFocus();", true);

 

that’s It now the auto focus is always set to this button in a page

Advertisements

About kakani santosh kumar

Working as Software Engineer in Prithvi Information Solutions having 3 years of Experience in Dotnet Framework ranging from 1.1 to 3.5
This entry was posted in Articles, Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s