Using Multiple ScriptManagers
I have an asp.net 4.0 app where all pages use a master page. I have several dynamic aspects going on in both the masterpage and the child of the pages. For both I'd like 开发者_开发技巧to be able to use the built in Microsoft ScriptManager with this kind of pattern:
Code behind:
using System;
using System.Web;
using System.Web.Services;
namespace MyApp.Secure
{
public partial class myPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetString()
{
return "abc";
}
}
}
Then in the page content itself:
<script>
PageMethods.GetString(OnSucceeded, OnFailed);
function OnSucceeded(rslt) {
alert('cool,worked.');
}
function OnFailed(error) {
alert('ooops,failed.');
}
</script>
So, to use this technique I'd like to expose static [WebMethod]'s in both my masterpage and my child page. My question is, how do I manage this with a ScriptManager? From my understanding, I need to have a ScriptManager within a form tag (why?) so that a javascript library is added to allow me to use the PageMethods object. Should I just create one ScriptManager in my masterpage and tuck it in a useless form tag in the masterpage? Will that ScriptManager pickup the [WebMethod] functions in my child page? And what about that form tag: I know I can only have one form runat='server' tag so how would I manage a that with other forms I need within my masterpage? Like my little form that wraps my search function at the top of the page?
Thanks
A ScriptManager on your masterpage will suffice (yes in the form tag). The on pages that inherit your master, place a scriptmanagerproxy control.
I think typically people use one form tag, and then for form buttons, the postback handles the action that is appropriate - like redirecting to a search page. I'm not a fan of this method, why handle a postback when you just want to redirect to a search page with a querystring. I've used javascript mostly get get around it, either on button click change the form action to whatever is appropriate, or just override the submit button type all together and have javascript build your new url and change the location bar
In ASP.Net you can only have one form tag. What you need to do is place the form tag in the master page and have no form tags in any pages that use the maaster page. I put the ScriptManager right below the form tag in the master page as it will be seen by all the pages using the master page. Your search function must be inside the form tags and cannot have it's own form tag, it will use the form in the master page. using Update Panels you can isolate the seach textbox and button and do whatever you want in the code behind such as redirect to a search result page or something like that.
The question has become moot since I've abandoned using ScriptManager. I've been able to get ExtJS to post to my [WebMethod]'s directly without having to rely on any additional Microsoft overhead. I'm not sure why anyone that's already using a good javascript library (like ExtJs or jQuery) would take the ScriptManager route to all [WebMethod]'s. You certainly don't need it and it just locks you into specific requires for using tags that have nothing to do with your app.
精彩评论