且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AJAX方法调用中的RegisterClientScriptBlock

更新时间:2023-12-05 23:28:46

使用AJAX启用页面,你应该使用的ScriptManager注册脚本:

  ScriptManager.RegisterClientScriptBlock(页的typeof(我的页面)
    的MyScript,GoStuff(),真)

您可以用它来注册所有的脚本(原载,回传,回传AJAX)。

I am trying to RegisterClientScriptBlock in a method that is only called via an AJAX call. It doesn't appear to actually register the script on the page and I'm guessing this is because it's not actually reloading the entire page. Is there any way to register javascript on a page from within an ajax method call?

	protected void MyMethod(object sender, EventArgs e)
	{
		// This method only called via AJAX call

		Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "resize", "alert('here');", true);
	}

With AJAX enabled pages, you should use the ScriptManager to register scripts:

ScriptManager.RegisterClientScriptBlock(Page, typeof(MyPage), 
    "MyScript", "GoStuff()", true)

You can use this to register all your scripts (Original load, postback, AJAX postback).