This is a migrated thread and some comments may be shown as answers.

Setting Focus Problem

9 Answers 291 Views
Window
This is a migrated thread and some comments may be shown as answers.
Satheesh
Top achievements
Rank 1
Satheesh asked on 21 Apr 2008, 06:28 AM
I am using Master pages in my asp.net Application. I want to set the focus to textbox control  in one  of my asp.net page(i.e Content page) loading in rad window.


I am using Radwindow to load my Asp.net pages.

9 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 23 Apr 2008, 02:30 PM
Hi Satheesh,

What we suggest is to use the following script called on the window's OnClientShow event - the current script sets the focus to the first input element in the content page:

<script type="text/javascript">  
function OnClientShow(sender, eventArgs)  
{  
 sender.get_contentElement().getElementsByTagName("INPUT")[0].focus();  
}  
 
</script> 

If you still experience problems with the focus please open a support ticket and send us you runnable project. We will do our best to provide a solution.


Sincerely yours,
George
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
NEX
Top achievements
Rank 1
answered on 19 May 2008, 07:27 PM
Hi,

I am trying to use this solution to set the focus of a control with same scenario as the prior thread and I am get null error on the function call sender.get_contentElement.

sender.get_contentElement().getElementsByTagName("INPUT")[0].focus();     
     
 

I have the set event "ClinetOnShow" on the window object.

Thank you,

Alfred
0
Georgi Tunev
Telerik team
answered on 20 May 2008, 01:52 PM
Hello Alfred,

Please open a ticket and send me your full code so I can check it. I will get back to you with a solution right away.


Kind regards,
Georgi Tunev
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jim
Top achievements
Rank 2
answered on 05 Sep 2008, 02:29 PM
Is there a resolution for this, I am having the same problem.
get_contentElement is null.
0
Georgi Tunev
Telerik team
answered on 08 Sep 2008, 05:58 AM
Hello Jim,

Please provide more details about your exact setup and we will do our best to help you.


Best wishes,
Georgi Tunev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
rwozniak
Top achievements
Rank 1
answered on 07 Oct 2008, 03:22 AM
Has anyone come up w/ a solution to this issue? I've run into the same issue...get_contentElement() returns null.

This is such a basic need for a dialog...is there a working sample anywhere? I can't find one in the samples, and have been battling this for hours.

Ross
0
Georgi Tunev
Telerik team
answered on 07 Oct 2008, 08:07 AM
Hello guys,

First of all I would like to apologize for the misunderstanding. The code that George provided is correct, but it is used for setting focus in popups like radconfirm or radprompt.

To achieve the desired effect with RadWindow, you need to go trough the hierarchy and find the element that you need in the window - note that the code is executed in the OnClientPageLoad EventHandler:

<script type="text/javascript">  
function OnClientPageLoad(sender, eventArgs)     
{     
     sender.get_contentFrame().contentWindow.document.getElementById("TextBox1").focus();   
}     
</script> 
 
<telerik:RadWindow   
    ID="RadWindow1"   
    runat="server"   
    NavigateUrl="dialog.aspx"   
    OnClientPageLoad="OnClientPageLoad">  
</telerik:RadWindow> 
 

There is also another solution to this task - all you need to do is to hook to the content page body's onload EventHandler and to set the focus there. This way you will not have to call the code above for every RadWindow that is opened.

Once again, I apologize for the misunderstanding.

All the best,
Georgi Tunev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
rwozniak
Top achievements
Rank 1
answered on 07 Oct 2008, 01:33 PM
Although that does work, there are some issues with it.

a) It is not a flexible solution. If I have 50 dialogs all with different form fields, I would need to write 50 functions, each with a different text field id.

b) Because ASP.NET dynamically assigns ids, I don't necessarily know what the id of the field will be when I write the function. A text field with id MyTextField might end up with the id ctl02_MyTextField.

The only place where I know the id that will be assigned is in the code-behind of the ascx that I am loading in the dialog. My first attempt was to add an AJAX response script via the RadAjaxManager that called the function below, passing the ClientID of the control:

function SetControlFocus(id)
{
    var element = document.getElementById(id);
    element.focus();
}

The problem is that, for some reason that messes up the layout of the dialog when it loads. Everything gets shifted up and to the left, and some of the form is obscured...at least in Firefox 3.

The solution that I have in place is a variation of the function you posted:

            var textFields = sender.get_contentFrame().contentWindow.document.getElementsByTagName("INPUT");
            for (var i=0; i < textFields.length; i++)
            {
                if (textFields[i].id == "__VIEWSTATE")
                {
                    textFields[i+1].focus();
                }
            } 

This is a pretty serious hack, but it sorta works for now. It does seem that the text field I want is the one immediately following the viewstate field. However, this is still flawed. What if the first field on the form is a dropdown, and I want that to be the field with focus? I can't just assume that it will always be a text box.

It seems that there is a serious need for a more elegant, one-line-of-code solution to this matter.

By the way, although the solution I posted does seem to work in most cases, I have a dialog with a RadEditor, and for some reason the presence of that control messes up the focus. I see the focus go into the text field momentarily, then it is lost...so this is definitely not a foolproof solution.

Ross


0
Georgi Tunev
Telerik team
answered on 08 Oct 2008, 02:49 PM
Hello Ross,

Indeed, you are right. Please note however that the task itself (to access a control in an IFRAME and to execute some method on it, like focus()) is a general one and is not directly related to the RadWindow control.
In my reply I simply show one way to do this - there are other approaches that would fit in specific scenarios.
What I can suggest is to implement the desired logic not by using RadWindow, but a standard IFRAME - once you have your code working as expected, the same approach will work with RadWindow (that is basically an IFRAME) as well.

As for the problem with RadEditor's dialog, at this point I cannot tell what the reason for it might be, but if you open a support ticket and send us a sample project, we will check it right away.


Best wishes,
Georgi Tunev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Window
Asked by
Satheesh
Top achievements
Rank 1
Answers by
George
Telerik team
NEX
Top achievements
Rank 1
Georgi Tunev
Telerik team
Jim
Top achievements
Rank 2
rwozniak
Top achievements
Rank 1
Share this question
or