Link to home
Start Free TrialLog in
Avatar of viola123
viola123

asked on

How to set focus on submit button of a login help web page?

hi all,
i have a login help web page, it has a master page.
and in the content holder:
1.  there are two tables,
2. one table tblPwd is for "forgot your password",
3. and table tblLoginID is for "forgot your login id"
4. each table contains two text boxes: Login ID and Password
5. so there are two buttons, buttonPwd is for submitting "forgot your password", buttonLoginID is for submitting "forgot your login id"
6. when i click the txtPwd in tblLoginID, the buttonPwd in tblPwd is highlighted automatically. i think it means buttonPwd in tblPwd is set to default submit button by Asp.net.
7. Question is: how to make buttonLoginID in tblLoginID highlighted when i go to(eg. focus on, enter/type words) the textboxes in tblLoginID? and make buttonPwd in tblPwd highlighted when i go to(eg. focus on, enter/type words) the textboxes in tblPwd?

thanks a lot
viola
Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

Through Javascript you can do this. Add a javascript function on the click or getfocus event of textbox. That javascript function will set the foucs on the required button.

Anurag
Avatar of viola123
viola123

ASKER

hi,
i am not a javascript expert, could you pls give me a sample code?

thanks
   <script language="javascript">
        function SetFocus()
        {
           btnSubmit.focus();
        }
    </script>

Call this method where ever you want to set the focus after the event.
hi,
it does not work for this case.
You can't use ".focus()" because when you click on any textboxes, the focus changes and you can't enter any text.
You can use one of the functions (I mean "WebForm_FireDefaultButton") in "WebForms.js" which is an embedded resource inside System.Web.dll.
So add the following codes to Page_Load.
Change the following names and correct them:  "TextBox1InTable1", "TextBox2InTable1", "TextBox1InTable2", "TextBox2InTable2", "ButtonNameInTable1" and "ButtonNameInTable2".

Now enjoy it... ;)
        ClientScript.RegisterClientScriptResource(typeof(Page), "WebForms.js");
        TextBox1InTable1.Attributes.Add("onkeypress", "javascript:return WebForm_FireDefaultButton(event, 'ButtonNameInTable1')");
        TextBox2InTable1.Attributes.Add("onkeypress", "javascript:return WebForm_FireDefaultButton(event, 'ButtonNameInTable1')");
        TextBox1InTable2.Attributes.Add("onkeypress", "javascript:return WebForm_FireDefaultButton(event, 'ButtonNameInTable2')");
        TextBox2InTable2.Attributes.Add("onkeypress", "javascript:return WebForm_FireDefaultButton(event, 'ButtonNameInTable2')");

Open in new window

I mentioned if you want to highlight the new button (change focus) you will not able to type immediately, so if it is important for you to change the face of active submit button, then change style and work on it.
hi,
it is not working properly.  when i click txtbox in table2, the button1 is still be highlighted.

thanks
ASKER CERTIFIED SOLUTION
Avatar of viola123
viola123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial