I using Vs2008/3.5 framework and creating new web application and when i tested a Telerik example code in that solutions,i'm getting a script error that RadAjaxManager is null when i use the following code.
$find(
"<%= RadAjaxManager1.ClientID %>").ajaxRequest();
even i replaced web.config with Telerik example web.config.still i m getting error.
what could be the reason?
4 Answers, 1 is accepted
<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" %>
<%
@ 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">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<div>
<asp:Button ID="Button1" runat="server" Text="Using AjaxManager"
onclick="Button1_Click" />
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:Button ID="Button3" runat="server" OnClientClick="javascript:callAjax()"
Text="Call AjaxRequest through Javascript Function" />
</div>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="Button1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="TextBox1" />
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="Button2">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="AccessDataSource1"
GridLines="None">
<
MasterTableView DataSourceID="AccessDataSource1">
<
RowIndicatorColumn>
<
HeaderStyle Width="20px"></HeaderStyle>
</
RowIndicatorColumn>
<
ExpandCollapseColumn>
<
HeaderStyle Width="20px"></HeaderStyle>
</
ExpandCollapseColumn>
</
MasterTableView>
</telerik:RadGrid>
<asp:AccessDataSource DataFile="~/App_Data/Nwind.mdb" ID="AccessDataSource1" runat="server"
SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [Title] FROM [Employees]">
</asp:AccessDataSource>
<asp:Button ID="Button4" runat="server" Text="Post back the page"
onclick="Button4_Click" />
</form>
</
body>
<script language="javascript" type="text/javascript">
function callAjax()
{
$find(
"<%= RadAjaxManager1.ClientID %>").ajaxRequest("Rebind");
}
</script>
</
html>
and in the code behind,
using
System;
using System.Data;
using System.Web.UI;
using Telerik.Web.UI;
namespace WebApplication6
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
TextBox1.Text =
"NotPostBack";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text =
"NotPostBack";
BindGrid();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add(
"Account");
dt.Columns.Add(
"AccountNO");
DataRow dr = dt.NewRow();
dr[
"Account"] = "Account1";
dr[
"AccountNO"] = "123456";
dt.Rows.Add(dr);
RadGrid1.DataSource =
null;
RadGrid1.DataSourceID =
null;
RadGrid1.DataSource = dt.DefaultView;
RadGrid1.Rebind();
}
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
BindGrid();
}
protected void Button3_Click(object sender, EventArgs e)
{
BindGrid();
}
protected void Button4_Click(object sender, EventArgs e)
{
TextBox1.Text =
"PostBack";
}
}
}
<asp:Button ID="Button3" runat="server" OnClientClick="javascript:callAjax()" Text="Call AjaxRequest through Javascript Function" />
Try adding return false; to the onclientclick (see below). The reason is that if you click that button right now it will immediately call your client script but it will also postback (perhaps cancelling your client script).
OnClientClick="javascript:callAjax();return false;"
I'm always forgetting to add that for server-side asp:buttons that only call client script (or ajax from client script).
Also I would move your function callAjax() Javascript inside of the </form> tag and surround the <script> with a <telerik:RadCodeBlock> (Actually if you surround it with a telerik:RadCodeBlock tag you could move it back into the <head> section were javascript belongs.
Hope that helps...
now i can able to do without postback, but still i'm facing one pbm, it's not updating the radgrid/page.
i just used following code in Ajax request event,
TextBox1.Text = "Through Ajax";
BindGrid();
And anytime in any other event you can just call RadGrid1.Rebind() which looks again at the need datasource and then rebind's the grid. See Here for simple example.
but..
If you knew all that and your page still isn't working, it could be that your controls aren't also inside either the RadAjaxManager or RadUpdatePanel (whichever you are using). Anything you change in an ajax request must be surrounded by your RadUpdatePanel or have a trigger setup for the target controls in order for you to see any change from the server-side (otherwise you won't actually see your change until after your next postback.)
Hope that helps.