Question: maintaining focus between pages

M

Mike

I have a few textbox controls that have autopostback so that when they loose
focus they update a label control that shows the count of characters in
their respective text control. This works fine, except that after the
postback, the page is shown reset -- scrolled to the top and with no control
having focus. Can I maintain scroll/focus w/o writing client-side
scripting?

thanks
 
R

Raterus

Not to question you as I'm sure you have a good reason, but why are you trying to avoid client-side code in this situation? Doesn't this problem beg for it?

To answer your question, no, you can't maintain scroll/focus w/o writing client-side code. Sure you can write code to do it on the server, but it is still going to be clientside code. I'd try to avoid the postback as much as possible, unless this is an intranet app and nobody will ever notice.

--Michael
 
M

Mike

Figured as much. This particular app. is for external users, but is a
management app. (not many users), not the main customer-facing application.
Basically there was an edict of "client code only when necessary" -- and
also laziness on my part. But basically, you're right.
So, any pointers to the preferred way to do this? (Figured there'd be a
declaritive way to do this and have the framework handle it, even if it
generated client scripting - but maybe not.)

thanks

Not to question you as I'm sure you have a good reason, but why are you
trying to avoid client-side code in this situation? Doesn't this problem
beg for it?

To answer your question, no, you can't maintain scroll/focus w/o writing
client-side code. Sure you can write code to do it on the server, but it is
still going to be clientside code. I'd try to avoid the postback as much as
possible, unless this is an intranet app and nobody will ever notice.

--Michael
 
M

Mike Newton

No, there isn't a way to do it with the framework.

Use Page.RegisterClientScriptBlock to place the javascript in the page,
technically you're still writing it on the server. :) Use the ID's as
references, since they are the same on the client side as they are on
the server side.

As for a pointer for info, use msdn. http://msdn.microsoft.com/library
is a good place to start. Under the Web Development tree....

Web Development->HTML and Dynamic HTML->SDK Documentation->Reference
will have everything that you basically need. There's an overview of
all things that appear in the HTML DOM, all properties, methods, and
yada. There is also some example scripting, but javascript is very
similar to typical server-side programming.
 
G

Guest

This is heavily edited, but you might be able to use something like this in
your base page class, if you have one.

private WebControl focusControl = null;

public void SetFocus( WebControl focus ) {
focusControl = focus;
}

private void BasePage_PreRender(object sender, EventArgs e) {
if ( focusControl != null ) {
Page.RegisterStartupScript( "FocusScript", string.Format(
"<script
language='javascript'>try{{document.getElementById('{0}').focus();}}catch(x){{}}</script>",
focusControl .ClientID );
}
}
 
J

Juno

Hi Mike,

Set smartNavigation to true.
Below is from MSDN:
In most circumstances, do not set this property in code. Set the
SmartNavigation attribute to true in the @ Page directive in the .aspx file.
When the page is requested, the dynamically generated class sets this
property.

When a page is requested by an Internet Explorer 5.5 browser, or later,
smart navigation enhances the user's experience of the page by performing
the following:

a.. eliminating the flash caused by navigation.
b.. persisting the scroll position when moving from page to page.
c.. persisting element focus between navigations.
d.. retaining only the last page state in the browser's history.
Smart navigation is best used with ASP.NET pages that require frequent
postbacks but with visual content that does not change dramatically on
return. Consider this carefully when deciding whether to set this property
to true.
 
C

Chandra Sekhar

I use a server side utility function to RegisterClientScript and set client
focus on html objects. I just do a window.attachevent and call a function
which places the focus on the control. Simple but very useful.

Sekhar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top