ASP.NET用户控件和AJAX
我有一个ASP.NET用户控件,作为其呈现阶段的一部分,它需要在客户端执行一些javascript。我使用ScriptManager.RegisterStartupScript注册响应到达客户端时要执行的脚本。现在,在我的场景中,我的控件包含在一个更新面板中,更准确地说,它是使用Telerik Manager进行ajaxified的。在同一页上,我有第二个更新面板,更准确地说,第二个面板与第二个RadAjaxManager设置进行了轴化。当第二个面板引发部分回发时,我希望忽略更改,包括从控件发出的任何脚本。事实并非如此,令人沮丧的是,我无法决定该怎么办?有什么想法吗?想法1:停止使用UpdatePanel。 ASP Webforms在很多方面都很好…部分页面呈现不是其中之一。虽然UpdatePanel在技术上执行部分页面呈现,但它也执行完全回发 相反,使用这个替代方案。 创建ASP Web API并重新控制页面加载过程。通过在服务器上创建一个web API控制器,它几乎只是一个可以使用javascript调用的函数,您可以将特定于服务器的信息传递给服务器,只运行您正在执行的操作所需的一个或多个函数,然后返回您需要在客户端上显示的确切数据 调用服务器的相同javascript函数(即,您的ASP Web API控制器)也将处理返回的响应,该响应很容易以易于使用的JSON格式格式化。最后,您的javascript将如下所示:ASP.NET用户控件和AJAX,asp.net,ajax,Asp.net,Ajax,我有一个ASP.NET用户控件,作为其呈现阶段的一部分,它需要在客户端执行一些javascript。我使用ScriptManager.RegisterStartupScript注册响应到达客户端时要执行的脚本。现在,在我的场景中,我的控件包含在一个更新面板中,更准确地说,它是使用Telerik Manager进行ajaxified的。在同一页上,我有第二个更新面板,更准确地说,第二个面板与第二个RadAjaxManager设置进行了轴化。当第二个面板引发部分回发时,我希望忽略更改,包括从控件发出
$('.show-kitten-button').click(function () { // this binds the click event whatever button begins the action.
// then we'll store some values in our variables
var kittenType = $('input.kitten-type').val(); // "calico" ...from asp textbox with a CssClass="kitten-type" attribute.
});
// function to make the call to the ASP Web API controller.
function getKittenPicture(kittenType) {
$.ajax({ // jQuery's AJAX function works well
type: "GET" // there are four types of requests: get, post, update, and delete. You can do most actions with get and post.
, url: 'http://yourWebSite.com/api/kittens/' + kittenType // the URL that routes to your ASP Web API controller. This is specified in the global.asax file.
, success: function (data) { // handle the data if the HTTP response indicates success
if (data || false) { // if data exists
var jsonData = JSON.parse(data); // parse it as JSON. This is assuming you are returning a serialized class object from C# or VB.NET. See below for more info on this.
var kittenImageSrcUrl = jsonData.src; // grab the data from the JSON object.
showKittenImage(kittenImageSrcUrl); // run the function to show the kitten image.
}
}
, error: function (data) { // handle the data if the HTTP response indicates an error.
console.log('An error occurred while retrieving the cute kitten picture.');
console.log('Error response: ', JSON.parse(data.responseJSON));
}
, complete: function (data) {
// any code to perform once the success (or error) function is complete.
}
});
};
function showKittenImage(imgSrc) { // function to show cute kitten image
$("#kittenImage").attr("src", "kittenImageSrcUrl"); // change the src attribute of the image to match the src URL we got back from the server.
};
更多信息
如果您还没有涉足API领域,那么您将得到一个真正的享受,因为它比使用UpdatePanel更容易、更有效。UpdatePanel乍一看似乎很简单,制作一个并没有多少困难,它们确实提供了动态页面内容的外观,但一旦您开始添加多个面板、插入启动脚本等,它很快就会变得毛茸茸的。下面是构建第一个API所需的所有内容的链接。我鼓励你去看看
谢谢你的建议。我将研究一下您建议的资源,然后返回给您!