 |
|
 |
with the deprecation of SmartNavigation, I'm looking for a workaround that will still allow me to "showModalDialog" and keep the postback in the same page - have you manged to do this? Note that replacing SmartNavigation with either the page directive or Page.MaintainScrollPositionOnPostBack just doesn't cut it, and yes I could do it via ajax but I'm stuck in this place at the moment.
John
|
|
|
|
 |
|
 |
I've the following piece of code in at class level, so i can access it anywhere in that class
private const string SCRIPT_DOFOCUS =
@"window.setTimeout('DoFocus()', 1);
function DoFocus()
{
try {
document.getElementById('REQUEST_LASTFOCUS').focus();
} catch (ex) {}
}";
This follwoing code is being used in checkbox event "chkVehiclesOwned", what i am doing here is that im keeping the focus on the same checkbox after it postbacks but i don't know why ain't it working. the same code is working fine in other checkbox events. Notice one thing that the client id i've used in the script it something like "ctl00_ContentPlaceHolder1_chkVehiclesOwned" this is because the checkbox gets new id in master pages n the above id has to be used to get your work done. Dude for last few days im stuck into it n i dont know what to do :S please help. Thanks
if (chkVehiclesOwned.Checked == true)
{
txtVehiclesOwned.Enabled = true;
}
else
{
txtVehiclesOwned.Enabled = false;
}
ScriptManager.RegisterStartupScript(this, typeof(Survey), "DoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", "ctl00_ContentPlaceHolder1_chkVehiclesOwned"), true);
|
|
|
|
 |
|
 |
First off, thanks for the nice example.
I am currently working on a web application which uses Anthem.net rather than AJAX (this project was started by someone else before MS AJAX/Atlas were available - unfortunately).
I have a number of dropdownlist, radiobuttonlist etc. controls which perform automatic callbacks. Setting focus in ASP.NET 2.0 on page load is easy enough, but for the purpose of enabling a user to use only their keyboard to fill out a large form, maintaining focus between call- or postbacks is essential.
When a page first loads in IE6, selecting a value using either mouse or keyboard from a dropdownlist causes IE to lose focus. Subsequent selects from the same list, however, and the focus stays intact. Whoever thought that up deserves to be beaten with a stick (you can tell this really annoys me).
Anyway, so your nice example solved that, and I thought I was in the clear. In Firefox, however, when you select something using the keyboard, the actual call- or postback only happens after the control loses focus. So when i select something, as soon as I press tab the callback happens, and focus is set to the control that I just tabbed away from. In other words, focus is always one behind the control that should be focused.
Now I know this is not a flaw in your code, but rather YET ANOTHER horrible browser inconsistency. I wonder if anyone can suggest a workaround?
Another thing i'd like to point out:
ASP controls such as radiobuttonlists, checkboxlists unfortunately do not render attributes for each of their containing controls. So although something like RadioButtonList.Atrributes.Add("onfocus" .... will not generate any errors, the resulting radio buttons on the page will not have this attribute set. This is a known issue - the only way around it is to override each of these control's render methods.
Any comments welcome
|
|
|
|
 |
|
 |
Hi,
the Problem is apparently that the onfocus event of the next control will not be called prior to postback. IE and FireFox have not exactly the same behaviour regarding events. Please check exactly which event calls the postback in Anthem.net framework using FireFox (in can be that because of some technical issue initiating of Postback in IE and FireFox are bound to different events). After you have it we can think about a workaround.
|
|
|
|
 |
|
 |
Hello,
I had a quick look - it seems Anthem fires the callback on the onchange or onclick events depending on the type of input. Keep in mind that when using the mouse the behaviour is the same in both IE/FF, eg: for a dropdownlist the postback occurs as soon as the selectedindex changes. When using the keyboard IE calls the postback on every change of the selectedindex, FF only when the control loses focus.
|
|
|
|
 |
|
 |
Hi,
How can maintain focus for dynamic created user controls?
regards
Christian
|
|
|
|
 |
|
 |
Where do you create contrals dynamically? In which method? Do you create them every time or just if !IsPostback?
Try to place the following call
HookOnFocus(this.Page as Control);
after you created the dynamic controls.
If you have just few of them than call
HookOnFocus(MyDynamicControl as Control);
for each of them.
Tell me if it helped. Otherwize please post an examle code, because it does metter how and where do you create this controls.
|
|
|
|
 |
|
 |
Hi George,
I temporally post my little asp.net project on my website.
Perhaps can you find out what the problem is with my code.
>>Project<<
It's a WAP-Project, which is supported since VS2005 SP1.
I also included a another href="http://www.codeproject.com/useritems/Dynamic_Control.asp" rel="nofollow">project from codeproject
Perhaps this conflicts with your solution.
regards
Christian
PS: Grüße aus Mainz |
|
|
|
 |
|
 |
Hi
I tested my solution without the update panel and your
approach is working without problems.
So the problem relates to Ajax. I will report any
further results.
regards
Christian
|
|
|
|
 |
|
 |
Hi, dein Archiv lässt sich nicht öffnen. Kannst direkt an meiner Email adresse schicken. Grüße aus Nürnberg.
|
|
|
|
 |
|
 |
http://www.sharptools.de/
Ich hoffe, dass es jetzt funktioniert. Habe es zumindest erfolgreich getestet.
MfG
Christian
|
|
|
|
 |
|
|
 |
|
 |
You are right! It will work in this special case, but:
1. What if you do not have AJAX at all? If you have a mixed solution?
2. What to do with controls which have no event handlers and must preserve focus as well?
3. Scalability? Each additional control on the page will cause an additional line of code.
Sorry, but "Thats not at all!".
|
|
|
|
 |
|
 |
Further to the above with a slight modification:
ScriptManager.GetCurrent(Me.Page).SetFocus(MyControl)
is what I found to handle this problem. The set up in my project would fall into case 2 of the author's solution and I have found this to work perfectly so far.
This modification allows you to use this with masterpages, which I was unable to do before as there wasn't a scriptmanager on each page.
|
|
|
|
 |
|
 |
Is there a way to put cursor at the end of the text at the TextBox after this script, not at the beginning?
Thank you!
|
|
|
|
 |
|
 |
See this Post http://forums.asp.net/2/1571377/ShowThread.aspx[^]
That might be help:
function moveFocusToEnd (ctlID) {
var textInput = $get(ctlID);
if (textInput.createTextRange) {
var range = textInput.createTextRange();
range.moveStart('character', (textInput.value.length));
range.collapse();
range.select();
}
}
|
|
|
|
 |
|
 |
Easier than that is to simply do:
textInput.value = textInput.value;
It moves the cursor to the end. Neat little trick!
|
|
|
|
 |
|
|
 |
|
 |
I have tried your workaround and I have found a problem. When you move from TextBox1 to TextBox2, a postback is done with __LASTFOCUS having by value TextBox2. This means if you are in TextBox3 when the server responds, the focus is back to TextBox2.
I have tried a variation. Instead of storing last focus in hidden field __LASTFOCUS, I store it in a JavaScript global variable. And instead of adding onfocus handlers server side, I add them client side.
Then, instead of calling HookOnFocus, I register the following startup script with ScriptManager:
function focusf() {
try{
ElementFocus=this.id
}catch(e){}
}
var inputs = document.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++)
$addHandler(inputs[i], 'focus', focusf);
And we can add handlers to other elements. Why register startup script with ScriptManager and not Page.ClientScript? Because when the content of an UpdatePanel is updated, the handlers inside the content are lost and then we need to add them again.
And instead of:
private const string SCRIPT_DOFOCUS =
@"window.setTimeout('DoFocus()', 1);
function DoFocus()
{
try {
document.getElementById('REQUEST_LASTFOCUS').focus();
} catch (ex) {}
}";
I have:
private const string SCRIPT_DOFOCUS =
@"window.setTimeout('DoFocus()', 1);
function DoFocus()
{
try {
document.getElementById(ElementFocus).focus();
} catch (ex) {}
}";
Now, the Replace isn't necessary.
The advantages:
* Less load on the server.
* Less data transfered, since there isn't the onfocus code in each tag.
* Since now the JavaScript snippets are constants, we can encapsulate them in JavaScript functions, move them in a js file and register a simple function call. This may imply simplicity and less data transfered, since the js file could be cached by the browser.
The disavantages:
* If there are a lot of elements to add the handler, the focus could be not saved when the user sets the focus in a element after an update.
|
|
|
|
 |
|
 |
Thank you very much. Your solution is brilliant for 100% AJAX applications, but if you have a normal (non AJAX) web page or a mixed page, your global javascript variable will lose its value and refocusing will fail.
Under mixed page i mean a page having an update panel with a number of controls in it and a control outsiede update panel which causes a postback. In this case your script will only work for controls inside.
|
|
|
|
 |
|
 |
Hey,
This is a very helpful piece of code.
But when adding it to my source, the problem is still there when posting back the first control.
After that, everything goes fine.
The first time I do a postback, the field Request["__LASTFOCUS"] is empty.
Any reason?
Any idea what I am doing wrong?
Thanks for the advise,
Kristien
|
|
|
|
 |
|
 |
Hi...
I'm using Master Page for Builing my Web Site.
Your Script is work perfectly "independent" aspx file.
However, applying master page, that script doesn't work.
I'm trying to figure out how it will work..
But, It's really difficult to me.
Please, Help.
|
|
|
|
 |
|
 |
Ha ha,
taht was a little bit tricky. Thank you!
1. Palce ScriptManager as a first control on the MasterPage
2. Place an UpdatePanel on each child page.
3. Place controls in UpdatePanels and set apropriate properties and events.
4. Include following code in MasterPage:
The difference is here:
ScriptManager.RegisterStartupScript(
Page,
Page.GetType(),
"ScriptDoFocus",
SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request["__LASTFOCUS"]),
true);
We say ScriptManager.RegisterStartupScript(Page, Page.GetType(), ...) instead of ScriptManager.RegisterStartupScript(this, this.GetType(), ...). The object this is the MasterPage and the object Page is the Child Page. So the problem was that with thisthe Script was rendered only once and with Page it will render every time the UpdatePanel updates.
Please try if it works and let me know.
|
|
|
|
 |
|
 |
Thanks..for your help..
I dont' believe that such many programmer in Microsoft couldn't think like you!!..
ajax and focus form is really important!!
You are better than any other programmer in Microsoft, i think.
Here is the Result.
You said.
1. Palce ScriptManager as a first control on the MasterPage
2. Place an UpdatePanel on each child page.
3. Place controls in UpdatePanels and set apropriate properties and events.
4. Include following code in MasterPage:
So, I placed ScriptManager as a first control("below the form tag"-any other location causes error. ) on the MasterPage.
and then, Place an UPdatePanel on my child page.
and then, do the number 3.
Number 4, Include following code in MasterPage.
There was a error, But I found how to fix that.
private const string SCRIPT_DOFOCUS =
@"window.setTimeout('DoFocus()', 1);
function DoFocus()
{
try {
document.getElementById('REQUEST_LASTFOCUS').focus();
} catch (ex) {}
}";
above part should be resides "master page" and "child page" both.
Because, without child page there was a error.
And, I thought Light-Weight Master. (less code, you know, not every page require textbox. Yet Inserting Main Script to Master Page is some kind useful.) So, I inserted all code to Child Page.
It works!!
Thanks...Thanks..Thanks..
Thank you for helping me.
Have a ni~~~~~ce day~!
|
|
|
|
 |
|
 |
I'm really appreciated that.
This article is exactly what I was looking for.
However, I have unsolved problem similiar one..
Could you help me, please?
Ok..
I have 2 textbox.
One is in the UpdatePanel(TextBox1), the other is normal asp server control textbox(TextBox2).
I want to Focus when textbox1_changed event fires..
like below.
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(some condition true)
{
TextBox1.Text = "";
TextBox1.Focus();
}
else
{
TextBox2.Focus();
}
}
It means, I want to Focus back to TextBox1 if some condition is true...
I need your help..Please, help.
Thanks...Have a nice day~!
|
|
|
|
 |