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

$find not working

2 Answers 419 Views
Input
This is a migrated thread and some comments may be shown as answers.
SSirica
Top achievements
Rank 2
Iron
Iron
Veteran
SSirica asked on 20 Mar 2012, 05:52 PM
I have created the simplest form I can to test setting a RadTextBox using javascript.

aspx:
<head runat="server">
<title>Textbox Page Load Testing</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="SCRM">
</asp:ScriptManager>
<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" DecoratedControls="All"
EnableRoundedCorners="True" />
<div>
<telerik:RadTextBox ID="txtTest" runat="server" Skin="" TextMode="SingleLine">
</telerik:RadTextBox>
</div>
<telerik:RadCodeBlock runat="server" ID="RadScriptBlock1">
<script type="text/javascript">
function setVal(val) {
$find("<%=txtTest.ClientID %>").set_value(val);
}
</script>
</telerik:RadCodeBlock>
</form>
</body>
</html>

VB:
Partial Class Default4
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ClientScript.RegisterStartupScript(Me.GetType(), "Pass", "setVal('TestValue');", True)
    End Sub
End Class

I get the following error:
Microsoft JScript runtime error: Unable to get value of the property 'set_value': object is null or undefined

Yes I have to do it on page load.
Yes I have to do this using javascript.

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 21 Mar 2012, 06:22 AM
Hello,

In ASP.NET AJAX environment, the ASP.NET AJAX controls (including RadControls for ASP.NET AJAX) are created after the page has been loaded. That's why you are not getting client side object of RadTextBox.
All ASP.NET AJAX controls however (including ours), are registered on the page on a later stage - in Sys.Application.Load - you can easily verify that by putting ASP.NET AJAX controls on a page and examining the HTML dump once the page is loaded in the browser. Basically, what happens is that your code is outputted and executed on the page, before the Ajax controls are rendered, which causes the error. Try
the following code to eliminate the error.
C#:
protected void Page_Load(object sender, EventArgs e)
 {
  string script = "<script language='javascript'>function f(){setVal('TestValue'); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
  Page.ClientScript.RegisterStartupScript(this.GetType(), "Pass", script);
 }

Another approach is that you can add the client side object of RadTextBox when page loads(OnLoad) and inside that you can access the client side object for each control. Try the following approach.
ASPX:
<telerik:RadTextBox ID="txtTest" runat="server" Skin=""  TextMode="SingleLine" ClientEvents-OnLoad="OnLoad">
</telerik:RadTextBox>
JS:
<script type="text/javascript">
 function OnLoad()
  {
   $find("<%=txtTest.ClientID %>").set_value('TestValue');
  }
</script>

Thanks,
Princy.
0
SSirica
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 21 Mar 2012, 12:46 PM
That worked.  Thanks Princy.
Tags
Input
Asked by
SSirica
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Princy
Top achievements
Rank 1
SSirica
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or