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

Set focus on page load from javascript

5 Answers 1240 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Santosh
Top achievements
Rank 1
Santosh asked on 04 Jan 2012, 02:53 PM
Hello Team,

I want to find telerik radnumerikbox in javascript on pageload and set the focus. I can't able to find the Id of element in javascript .

<script type="text/javascript" >
       function pageLoad(sender, args) {
           try {
               $find("<%# txtDay.ClientID %>").focus();
           }
           catch (e) {
           }
       }
</script>

For this I did the above code but its not working.

Please help me.

 

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Jan 2012, 05:37 AM
Hello Santosh,

Try accessing the TextBox as shown below.
JS:
function pageLoad(sender,args)
{
 var txt= $find("<%= txtDay.ClientID %>").focus();
}

-Shinu.
0
Santosh
Top achievements
Rank 1
answered on 05 Jan 2012, 02:40 PM

Hi Shinu,

I tried this also but it gives me below error.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).


Thanks,
Santosh
0
Richard
Top achievements
Rank 1
answered on 05 Jan 2012, 05:59 PM
Santosh:

I'm not able to reproduce the error when using FireFox 9.0.1, IE 9.0.4 or Chrome. The following code works without error:

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript">
        function pageLoad(sender, args) {
            var txt = $find("<%= txtDay.ClientID %>");
            txt.focus();
            //alert("test");
        }
    </script>
 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <div>
        <telerik:RadTextBox runat="server" ID="txtName" >
        </telerik:RadTextBox>
        <telerik:RadNumericTextBox Runat="server" ID="txtDay" >
        </telerik:RadNumericTextBox>
    </div>
    </form>
</body>
</html>
 
I added a RadTextBox prior to the RadNumericTextBox to be sure that the second one recieves focus on page load. It does do so.

See the screenshot for the display with Firebug enabled.

Alternatively, you could use a RadCodeBlock to wrap your JavaScript. See the RadCodeBlock and RadScriptBlock document page which does offer this as a solution for this exact error.

Hope this helps!
0
Santosh
Top achievements
Rank 1
answered on 06 Jan 2012, 05:27 AM
Hey Hi,

Thanks for your reply.  It works when I put that java script function inside <telerik:RadCodeBlock></telerik:RadCodeBlock>
It work for me in FF and Chrome but its giving me problem on IE8.

I have used Rad Window for this. Is the problem because of Radwindow?

Thanks,
Santosh
0
Plamen
Telerik team
answered on 09 Jan 2012, 01:37 PM
Hello Santosh,

Please try to use the following code:

<script type="text/javascript">
      var obj;
      function pageLoad(sender, args) {
          obj = $find("<%= txtDay.ClientID %>");
      }
 
      function OnWindowShow(sender, args) {
          setTimeout(" obj.focus();", 200);
      }
 
  </script>
  <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
  </telerik:RadAjaxManager>
  <div>
      <telerik:RadWindow runat="server" ID="RadWindow1" VisibleOnPageLoad="true" OnClientShow="OnWindowShow">
          <ContentTemplate>
              <telerik:RadNumericTextBox runat="server" ID="txtDay">
              </telerik:RadNumericTextBox>
          </ContentTemplate>
      </telerik:RadWindow>
  </div>

Hope this helps.
 
Kind regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
General Discussions
Asked by
Santosh
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Santosh
Top achievements
Rank 1
Richard
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or