-
May 31st, 2011, 05:28 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Jquery to PageMethods
Code:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
</asp:ToolkitScriptManager> <script language="javascript" type="text/javascript">
$(function () {
$(".mnuMainMenu_SkipLink a").each(function (index)
{
$(this).click(ClearCache(););
}
);
}
);
function ClearCache() {
PageMethods.ClearSearchCache();
}
</script>
I am trying to make jquery find my asp.net menu and add a click event on each one that will call some server side script.
However I keep getting the message Object expected. I am unable to tell whether it cant find my page method or my jquery is wrong?
-
May 31st, 2011, 05:46 AM
#2
Thread Starter
Frenzied Member
Re: Jquery to PageMethods
I simplyfied the function and still get the error
Code:
$(function () { $(".mnuMainMenu a").each(function (index) { $(this).click(function () { alert("fish");}); }); });
I am basically trying to copy this tutorial and adapt it for my needs
http://stackoverflow.com/questions/4...m-from-aspmenu
-
May 31st, 2011, 06:52 AM
#3
PowerPoster
Re: Jquery to PageMethods
it's probably trying to assign the click event before the DOM is ready. try putting that code inside the following function
Code:
$(document).ready(function() {
});
-
May 31st, 2011, 07:38 AM
#4
Thread Starter
Frenzied Member
Re: Jquery to PageMethods
Ok I solved the original error, I wasnt referencing the jquery library correctly, however the I dont think my code is finding the menu items as the alert still doesnt present its self.
I also added the code into the function as above.
Code:
<a href="#MainMenu_mnuMainMenu_SkipLink"><img alt="Skip Navigation Links" src="/WebResource.axd?d=_1nhm1HxiLCVEzCUEJg_xF7psxFSLCBcivpWJjmInnqGrey1YpUhgYK2XOZ684dF2-bOL7YIubQtVWwFBffiGONXTj41&t=634376242957102069" width="0" height="0" style="border-width:0px;" /></a><div id="MainMenu_mnuMainMenu">
<ul class="level1">
<li><a title="Help" class="level1" href="/Help.aspx">help and faq</a></li>
</ul>
</div><a id="MainMenu_mnuMainMenu_SkipLink"></a>
-
May 31st, 2011, 08:36 AM
#5
Thread Starter
Frenzied Member
Re: Jquery to PageMethods
After all that I got the jquery working but now it seems that PageMethods will not work when using Usercontrols or master pages, what a waste of time
Code:
<script type="text/javascript">
$(document).ready(function () {
$(function () { $("#MainMenu_mnuMainMenu").each(function (index) { $(this).click(function () { ClearCache(); return true; }); }); });
function ClearCache() {
alert("fishy");
PageMethods.ClearSearchCache();
}
});
</script>
-
May 31st, 2011, 09:45 AM
#6
Thread Starter
Frenzied Member
Re: Jquery to PageMethods
Ok its not as bad as expected most of the code i.e. the scriptmanager and the jquery can all go in the master page only the webmethod as below needs to go in the actual page and is apparently not allowed in the code behind. But could I just add a reference to it instead of repeating on each page?
<script language="C#" runat="server">
[System.Web.Services.WebMethod(CacheDuration = 60)]
public static void ClearSearchCache()
{
SearchResults sContent = SearchResults.GetContent();
if (sContent != null)
{
HttpContext.Current.Session.Remove("_SearchResults_");
}
}
</script>
-
Jun 1st, 2011, 01:52 AM
#7
Re: [RESOLVED] Jquery to PageMethods
If that method wants to be on every page, then you could create a Base Page class which includes it, and have all your ASP.Net Page inherit from that Base Page class.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|