 |
|
 |
I did not test this myself, but it could be usefull if the author could update the article with some load test metrics, comparing load times on a normal page (as benchmark) with load times on his custom solution.
I reckon that implementing this class could be quite expensive, especially when complex controls, such as datagrids are used. The author also uses recursion, which will use a lot of stack memory. This could cause a stack overflow on server side if not managed properly.
|
|
|
|
 |
|
 |
thanks a lot,
it work for me..thank you
|
|
|
|
 |
|
 |
After compiling and referencing this solution and modifying my System.Web.UI.Page to reference..... Webcontrols, when the EDIT link is clicked in my Datalist, the datalist is empty (had rows before click). If I switch back to System.Web.UI.Page, the problem is gone. Any ideas what might be going on here?
|
|
|
|
 |
|
 |
Sir
I did download the project and add the dll to my project. It is working correctly... but nw we designing the AJAX enabled websites, and putting the controls inside update panel, this functionality is not working for these pages... hope u will advise me to use it for ajax enabled website..
Thanks In Advance
Akbar
|
|
|
|
 |
|
 |
Is there a way to move focus to the next element in my form using the current element's informaton? I can't hardcode the next element's id since it could be different based on which form I am in. I tried nextSibling but that doesn't quite work I guess it works only on child nodes in a certain element?
I'm trying to do this in my onkeyup event since I need to do a bunch of other things when this event is triggered.
(it's a ASP.NET/VB.NET application & I use Javascript for event handling for dropdown boxes)
arvind
|
|
|
|
 |
|
 |
You won't know if it is the next control that needs to be focussed! The AutoPostBack also fires when the user pressed Shift-Tab (moving to the previous control) or used the mouse to select any other control...
Focussing the current control is the "least horrible" solution. Much better would of course be to use unobtrusive AJAX so no postback is needed at all to send form data back to the server.
|
|
|
|
 |
|
 |
I think I have found a very simple solution for all the focus trouble, at least for .NET 2.0. I didn't test it with 1.1, so it may work there too.
Just add this line of code for each control, that you want to include in the focus management.
MyControl.Attributes.Add("onfocus", "document.getElementById('__LASTFOCUS').value=this.id;");
The browser changes the focus of the element before the post pack, so the control which should get focus after post back is already focused for a short moment and the __LASTFOCUS is set correctly.
And now the big surprise: You must do nothing to get the correct control focused after post back. It's already done by the framework. I have no idea how it works, since I couldn't find a line of script that does the job. Looks like there was some focus management planned for .NET, but not completely implementet. Very strange, since only such a small bit seems missing.
I testet just with Firefox 2.0 an IE 7.0. If someone else tests some other browsers, it would be nice to drop my a note or add an comment right here.
|
|
|
|
 |
|
 |
.NET 1.1 the following is not working.
MyControl.Attributes.Add("onfocus", "document.getElementById('__LASTFOCUS').value=this.id;");
I thought I have got a one lineer solution. I will try in 2.0 when I migrate.
|
|
|
|
 |
|
 |
Simon,
I added the following code to your template for the datalist control. It compiles nicely but does not work. What am I doing wrong?. please help
else if (control is DataList)
{
//Not Tested
((DataList)control).DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(DatalistCommand);
((DataList)control).EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(DatalistCommand);
((DataList)control).UpdateCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(DatalistCommand);
((DataList)control).CancelCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(DatalistCommand);
}
and this method
private void DatalistCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
SetFocusControl(source);
}
Thanks Simon
Viju8744
|
|
|
|
 |
|
 |
Hi Viju8744,
Interesting Problem...
To get the controls on the DataGrid to maintain focus:
in LoadControlEvents()...
else if (control is DataGrid)
{
((DataGrid)control).ItemCreated +=new DataGridItemEventHandler(DataGrid_ItemCreated);
}
and elsewhere in the Page.cs:
private void DataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
{
LoadControlEvents(e.Item.Controls);
}
--
I hope this helps.
Cheers,
Simon
|
|
|
|
 |
|
 |
Hello Simon,
I use webmatrix as development tool (low cost), so I don't have a code-behind file.
Compiling and setting a reference worked well, but how do use this in vb.net?
JeffyMan
|
|
|
|
 |
|
 |
Hi Jeff
Sorry it's taken me a while to get back to you - I've been busy
I don't actually have webmatrix installed but I presume it would be the same as DevStudio for managing code-behind.
You should have some code that looks like this:
Public Class WebForm1
Inherits System.Web.UI.Page
change this to:
Public Class WebForm1
Inherits WebControls.Page
That should be it...
|
|
|
|
 |
|
 |
how does this then relate to SmartNavigation page property?
from MSDN:
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:
eliminating the flash caused by navigation.
persisting the scroll position when moving from page to page.
persisting element focus between navigations.
retaining only the last page state in the browser's history.
one advantage u have is the ability to set focus to another control other than the default [previously selected].
any others?
|
|
|
|
 |
|
 |
I am not sure how much you've worked with the SmartNavigation property but I have had a number of problems with it:
* Redirects not working,
* Client-side errors.
I don't want the advanced features of SmartNavigation (e.g. setting the scrollbar), just the focus. I also want to remain in control of the code and extend it to other browsers as required (not just IE 5.5).
So basically the other advantage is that your are in control of the code and can customize it as you need to.
There are workarounds for the SmartNavigation problems. If you are deploying an IE 5.5 solution, SmartNavagation will most likely serve you well but be aware you may run into some problems...
|
|
|
|
 |
|
 |
All of my web apps use an extended System.Web.UI.Page object. This extended object has a method
public void setFocus(System.Web.UI.Control control)
{
this.Page.RegisterStartupScript("focus","<script language=\"javascript\">\n<!--\nwindow.onload = setfocus;\n function setfocus()\n{\n\t\twindow.focus();\n\t\tif(document.all['" + control.ClientID.ToString() + "'])\n\t\t{\n \t\t\tdocument.all['" + control.ClientID.ToString() + "'].focus();\n\t\t}\n}\n// -->\n</script>\n");
}
Then I just call this method as needed from my code behinds.
Chuck Wagner
|
|
|
|
 |
|
 |
Chuck,
Thanks. It worked. Loving this solution. simple and humble. I was looking for this for quite some time. Seen several, but most of them were complicated.
(Chuck Wagner's code(as a response) was removed from here somehow)
Nandu
modified on Tuesday, December 11, 2007 5:13:39 PM
|
|
|
|
 |
|
 |
Hi,
Whenever postback events are fired, we find the the control that caused the postback looses the focus. This is iritiating from user experience perspective. To resolve this issue this is what we deployed in one of our applications:
In every ASPX page we called a javascript function such as
<body önload='BodyOnLoad("tbDateFrom");'>
Here tbDateFrom is the name of the control on which we want to focus when first time page is loaded. The same function can be attached through server side script only but I chose to keep it client side so that I can change the default control name at my will. The javascript function BodyOnLoad was coded as:
function BodyOnLoad(defaultControl)
{
// If postback then check whether we want to
// focus on an alternative control that had caused
// postback
if(IsPostBack && PostBackControl != undefined)
defaultControl = PostBackControl;
// Find the element and if it's not disabled
// set focus on the desired element
obj = document.getElementById(defaultControl);
if(obj != null && !obj.disabled)
obj.focus();
}
As you can see above, we are using the method to achieve two functionalities. First, we identify the default element on which focus should be set when the form is loaded. At the same time, we also use the same method to focus on the control that caused the postback. Here's the necessary code that we included in our page to implement the postback-control identification functionality:
Friend DefaultFocusControl As Control
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
' If programmer didn't set the default focus in case of server error
' check if we are in Postback mode. Try to set focus on the control
' that caused postback
If DefaultFocusControl Is Nothing AndAlso IsPostBack Then
DefaultFocusControl = GetPostbackControl(Me)
' If postback is through control bar such as on click of Add button
' then it shouldn't remain on the button bar control
If TypeOf DefaultFocusControl Is LinkButton Then
' Ignored some controls
If TypeOf DefaultFocusControl.Parent Is ControlBar OrElse TypeOf DefaultFocusControl.Parent Is MenuDock Then
DefaultFocusControl = Nothing
End If
End If
End If
If DefaultFocusControl Is Nothing Then
RegisterStartupScript("IsPostBackScript", " IsPostBack = true; ")
Else
RegisterStartupScript("IsPostBackScript", " IsPostBack = true;PostBackControl='" & DefaultFocusControl.ClientID & "'; ")
End If
End Sub
Function GetPostbackControl(ByVal targPage As Page) As Control
If targPage.IsPostBack Then
' try to find the name of the postback control in the hidden
' __EVENTTARGET field
Dim ctlName As String = targPage.Request.Form("__EVENTTARGET")
' if the string is not null, return the control with that name
If ctlName.Trim().Length > 0 Then
Return targPage.FindControl(ctlName)
End If
' the trick above does not work if the postback is caused by standard
' buttons.
' In that case we retrieve the control the ASP-way: by looking in the
' Page's Form collection
' to find the name of a button control, that actually is the control
' that submitted the page
Dim keyName As String
For Each keyName In targPage.Request.Form
Dim ctl As Control = targPage.FindControl(keyName)
' if a control named as this key exists,
' check whether it is a button - if it is, return it!
If Not ctl Is Nothing Then
If TypeOf ctl Is Button Then
Return ctl
End If
End If
Next
End If
Return Nothing
End Function
There's also another related article available at http://www.eggheadcafe.com/articles/20050609.asp[^]
The idea for the VB.Net function GetPostbackControl was picked from another website that I don't recall right now. I hope above helps. Let me know your views.
Thanks,
Durlabh
|
|
|
|
 |
|
 |
That looks really good.
I like the idea of getting the postback control without that ugly recursive algorithm.
I don't like using the .NET internal form tags like that (incase they change between framework versions) but for efficiency's sake, I think it works in this case.
Very cool - thanks for the tip - I'll provide a version of my code that incorporates the EVENTTARGET change.
|
|
|
|
 |