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

Setting focus to a TextBox in a RadWindow...

6 Answers 630 Views
Window
This is a migrated thread and some comments may be shown as answers.
mark baer
Top achievements
Rank 1
mark baer asked on 19 Apr 2011, 10:35 PM
I can't seem to get this to work.  I have a simple Login page with 2 textboxes.  I've tried all of the normal methods, plus a few things I've seen in this forum.  None of them are working.  I am using ASP.NET 4, VS 2010 and the LATEST build of the Telerik Controls.  My HTML for the page is listed below.  I've tried "OnClientShow"...the Javascript fires, but the button never visually looks like it has focus and no error appears.  I'm debugging with IE 8.

Help?

thanks

mark

 

<P><body class="form" ><BR>    <telerik:RadScriptBlock 
ID="RadScriptBlock1" 
runat="server"><BR>        <script 
type="text/javascript"><BR>//            
function OnClientShow(sender, eventArgs) 
<BR>//            
{<BR>//                
try 
<BR>//                
{</P>
<P>//                    
var textbox = document.getElementById("<%= txtUserName.ClientID 
%>");<BR>//                    
//var textbox = 
document.getElementById("RadWindow1$C$txtUserName");<BR>//                    
//for 
testing...<BR>//                    
//alert(textbox.name);</P>
<P>//                    
if (textbox != null) 
{<BR>//                        
//-----------------------------------------------------<BR>//                        
//This Code is firing, but the UI doesn't reflect 
it...<BR>//                        
//-----------------------------------------------------<BR>//                        
textbox.focus();<BR>//                        
//alert('Just Set 
Focus');<BR>//                    
}<BR>//                    
else 
{<BR>//                        
alert('Could not set focus to 
Textbox...');<BR>//                        
return;<BR>//                    
}<BR>//                
}<BR>//                
catch (e) 
{<BR>//                    
alert('Error setting focus:  ' + 
e.Message);<BR>//                    
return;<BR>//                
}<BR>//            
}   <BR>  <BR>        
</script><BR>    
</telerik:RadScriptBlock><BR>    <form id="form1" 
runat="server" defaultfocus="txtUserName"><BR><telerik:RadScriptManager 
ID="RadScriptManager1" runat="server"><BR>    
</telerik:RadScriptManager><BR>    <telerik:RadWindow 
ID="RadWindow1" runat="server" VisibleOnPageLoad="true" Behaviors="None" 
Skin="WebBlue" AutoSize="true" <BR>        
Title="Login to Creative Affairs!" VisibleStatusbar="False" 
><BR>        
<ContentTemplate><BR>            
<table width="275px" border="0" 
align="center"><BR>                
<tr><BR>                    
<td 
align="center"><BR>                        
<table class="formLabel" width="275px" 
cellpadding="3"><BR>                            
<tr><BR>                                
<td 
align="right"><BR>                                    
<asp:Label ID="Label1" runat="server" Text="User 
Name:"></asp:Label><BR>                                
</td><BR>                                
<td 
align="left"><BR>                                    
<asp:TextBox ID="txtUserName" runat="server" 
Width="125px"></asp:TextBox><BR>                                    
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
ControlToValidate="txtUserName"<BR>                                        
ErrorMessage="Required"></asp:RequiredFieldValidator><BR>                                
</td><BR>                            
</tr><BR>                            
<tr><BR>                                
<td 
align="right"><BR>                                    
<asp:Label ID="Label2" runat="server" 
Text="Password:"></asp:Label><BR>                                
</td><BR>                                
<td 
align="left"><BR>                                    
<asp:TextBox ID="txtPassword" runat="server" Width="125px" 
TextMode="Password"></asp:TextBox><BR>                                    
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
ControlToValidate="txtPassword"<BR>                                        
ErrorMessage="Required"></asp:RequiredFieldValidator><BR>                                
</td><BR>                            
</tr><BR>                            
<tr 
align="left"><BR>                                
<td><BR>                                    
&nbsp;<BR>                                
</td><BR>                                
<td><BR>                                    
<table 
width="100%"><BR>                                        
<tr><BR>                                            
<td align="left" 
width="60%"><BR>                                                
<asp:CheckBox ID="chkRemember" TextAlign="right" Text="Remember Me" 
runat="server" 
/><BR>                                            
</td><BR>                                            
<td 
align="right"><BR>                                                
<asp:Button ID="btnLogin" runat="server" Text="Login" 
OnClick="btnLogin_Click" 
/><BR>                                            
</td><BR>                                        
</tr><BR>                                    
</table><BR>                                
</td><BR>                            
</tr><BR>                            
<tr 
align="left"><BR>                                
<td 
colspan="2"><BR>                                    
<asp:Label ID="lblError" runat="server" ForeColor="Red" 
Text=""></asp:Label><BR>                                
</td><BR>                            
</tr><BR>                        
</table><BR>                    
</td><BR>                
</tr><BR>            
</table><BR>        
</ContentTemplate><BR>        
<BR>    
</telerik:RadWindow><BR>        
<BR>    </form><BR></body><BR></html><BR></P>

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Apr 2011, 10:09 AM
Hello Mark,

Check out the following forum which discussed the similar scenario.
Set focus to RadInput within RadWindow ContentTamplate on load

Thanks,
Princy.
0
Marin Bratanov
Telerik team
answered on 20 Apr 2011, 10:43 AM

Hello Mark,

A simple timeout seems to fix the issue on my end:

setTimeout(function ()
{
    textbox.focus();
}, 0);

I suggest that you may also try attaching to the OnClientAutoSizeEnd event, as resizing a window can take the focus off the desired element.



Best wishes,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Niska
Top achievements
Rank 1
answered on 14 Feb 2012, 08:28 AM
Hi Marin,

I did set focus on textbox control,

Now - I'm trying to know if radTextBox is focused - on client side,
the radTextBox.Focused rerturns 'false', but the "_focused" property holds 'true' (which is private property and I can't have this value)
is there any way to get the right indication about this textBox focusing??

It's very important to me!


thanks!
Niska. 
0
Richard
Top achievements
Rank 1
answered on 14 Feb 2012, 10:02 PM
Niskya:

There are some detailed insights provided in the Problem with RadTextBox.Focused in Telerik 2010Q1 forum thread response from Deyan (Telerik Admin) in his May 26, 2010 reply. These should point you in the right direction.

"The RadTextBox control hosts a standard TextBox control and extends its functionality by providing support for the Telerik Presentation Framework (theming, dependency properties etc.). Since the control itself is not the focused control (but the TextBox inside) the Focused property will not be set to true. However, you can use the ContainsFocus property. This property will always be true when a control inside the current control is focused."

Hope this helps!
0
Niska
Top achievements
Rank 1
answered on 15 Feb 2012, 10:16 AM
Thank you,
but the 'containsFocus' method is undefined on client side (aspx page)
is there any other way to solve my problem?

this is what I did:

var searchInput = $find("<%= txtSearch.ClientID %>");
if (searchInput.ContainsFocus)
    return;


Niska.
0
Svetlina Anati
Telerik team
answered on 15 Feb 2012, 01:13 PM
Hi Niska,

You can go ahead with the private _focused property, it should work correctly, despite this is not a public getter. I also logged in public getter and/or setter implementation to be considered at a later stage. 

Regards,
Svetlina Anati
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Window
Asked by
mark baer
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Marin Bratanov
Telerik team
Niska
Top achievements
Rank 1
Richard
Top achievements
Rank 1
Svetlina Anati
Telerik team
Share this question
or