I have a page in my ASP.NET 3.5 website that may display some error messages after the user has entered data in some textboxes and hit a 'Continue' button. The problem is that such messages may not be obvious if they appear in a lower part of the page than
the user is currently viewing. The need to scroll down the page is not self-evident in these circumstances.
In the event of an error, I want to move the focus to an anchor that is placed in a suitable location in the html markup but I don't want to trigger a postback, as that would wipe out the user input and the error message (I'm using an asp:UpdatePanel). Can
anyone suggest some code that would force this move to an anchor point?
Hi Dan,
That's good to know but I have no Javascript skills at present. I will start searching for some sample code but if you have any code suggestions of your own then they would be much appreciated.
I am not a full time programmer and I raised my original question after a break of about 9 months since I last did any serious ASP.NET coding. My skills were thus a little rusty. Last night, I realised that there was no need for me to develop new skills
in Javascript to resolve my problem, as there was a server side solution staring me in the face.
Moving focus to an anchor point was only a passing thought. Moving focus to a button was just as good a solution in my circumstances - and I suddenly remembered that buttons have a Focus method. Simply placing MyButton.Focus() at a relevant point in my code
did the job.
David
Marked as answer by haggis999 on Nov 16, 2012 12:47 PM
haggis999
Member
130 Points
611 Posts
Can I use code to move to an anchor without postback?
Nov 10, 2012 01:55 PM|LINK
I have a page in my ASP.NET 3.5 website that may display some error messages after the user has entered data in some textboxes and hit a 'Continue' button. The problem is that such messages may not be obvious if they appear in a lower part of the page than the user is currently viewing. The need to scroll down the page is not self-evident in these circumstances.
In the event of an error, I want to move the focus to an anchor that is placed in a suitable location in the html markup but I don't want to trigger a postback, as that would wipe out the user input and the error message (I'm using an asp:UpdatePanel). Can anyone suggest some code that would force this move to an anchor point?
David
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Can I use code to move to an anchor without postback?
Nov 10, 2012 03:12 PM|LINK
You can use javascript to move the focus.
haggis999
Member
130 Points
611 Posts
Re: Can I use code to move to an anchor without postback?
Nov 10, 2012 05:21 PM|LINK
Hi Dan,
That's good to know but I have no Javascript skills at present. I will start searching for some sample code but if you have any code suggestions of your own then they would be much appreciated.
David
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Can I use code to move to an anchor without postback?
Nov 10, 2012 06:37 PM|LINK
Gee, what a coincidence. I just made comments on this thread,
http://forums.asp.net/t/1857802.aspx/1?Javascript+Sum+HTML+table+cells+OnClick+into+text+box
that seem appropriate for your situation.
haggis999
Member
130 Points
611 Posts
Re: Can I use code to move to an anchor without postback?
Nov 12, 2012 09:23 AM|LINK
I am not a full time programmer and I raised my original question after a break of about 9 months since I last did any serious ASP.NET coding. My skills were thus a little rusty. Last night, I realised that there was no need for me to develop new skills in Javascript to resolve my problem, as there was a server side solution staring me in the face.
Moving focus to an anchor point was only a passing thought. Moving focus to a button was just as good a solution in my circumstances - and I suddenly remembered that buttons have a Focus method. Simply placing MyButton.Focus() at a relevant point in my code did the job.
David