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

[Solved] Ajax Postback on page load

1 Answer 294 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Brete
Top achievements
Rank 1
Brete asked on 08 May 2008, 02:17 PM
I am trying to have a ajax page initiate self -  postback after the page loads on the client side the first time.  The script raises a "object is null error" when it trys to run.  However if I ignore the error and manually click the button it works.     Here is a simplified sample of the code. 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestAjax2.aspx.cs" Inherits="TestAjax2" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body  >
    <form id="form1" runat="server">
    <div>
       <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
 
  <script language=jscript>
  function xpress(){
 
   
  $find('<%=RadAjaxPanel1.ClientID%>').ajaxRequestWithTarget("<%= RadAjaxPanel1.UniqueID %>", "Button1Press")
  }
  </script>
  </telerik:RadScriptBlock >
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">

    </telerik:RadScriptManager>
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="200px" Width="300px">
          
      
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
   
   
      <br />
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
     <input type=button id=buttonx   value="Change color" onclick='<%=RadAjaxPanel1.ClientID%>.ajaxRequestWithTarget("<%= RadAjaxPanel1.UniqueID %>", "ButtonxPress")' />
      <input type=button id=button1   value="Change colorx" onclick='xpress()' />
    </telerik:RadCodeBlock>
    </telerik:RadAjaxPanel>
    </div>
    </form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class TestAjax2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                Type cstype = this.GetType();
              
            //    ClientScript.RegisterStartupScript(cstype, "", "xpress();", true);

                ScriptManager.RegisterStartupScript(this.Page, cstype, "", "javascript:xpress();", true);
           

        }

    }
    protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
    {
        base.RaisePostBackEvent(source, eventArgument);
        Label1.Text = eventArgument;
    }
}




1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 May 2008, 02:46 PM
Hi Brete,

ScriptManager.RegisterStartupScript will render the script before component creation - you can use Sys.Application.load event instead:

http://www.asp.net/ajax/documentation/live/clientreference/Sys/ApplicationClass/SysApplicationLoadEvent.aspx

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Ajax
Asked by
Brete
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or