Javascript 如何调用母版页的web方法?
Jquery/Javascript:希望在通知\u count\u span淡出并返回false后调用updateNotification()函数。web方法位于母版页中Javascript 如何调用母版页的web方法?,javascript,jquery,asp.net,webmethod,Javascript,Jquery,Asp.net,Webmethod,Jquery/Javascript:希望在通知\u count\u span淡出并返回false后调用updateNotification()函数。web方法位于母版页中 <script type="text/javascript" > $(document).ready(function () { $("#notificationLink").click(function () { $("#notificationContainer"
<script type="text/javascript" >
$(document).ready(function () {
$("#notificationLink").click(function () {
$("#notificationContainer").fadeToggle(300);
$("#notification_count_span").fadeOut("slow");
return false;
// updateNotification()
});
//Document Click
$(document).click(function () {
$("#notificationContainer").hide();
});
});
function updateNotification() {
$.ajax({
type: "POST",
url: "SiteMaster.master/UpdateNotification",
data: '{nForId: "' + $("#<%=sessionUnameHf.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//alert(msg.d)
//do nothing
}
});
}
</script>
<asp:Label ID="notification_count_lbl" runat="server" Text=""></asp:Label></span>
<asp:LinkButton ID="notificationLink" runat="server">Notifications</asp:LinkButton>
<!--<a href="#" id="notificationLink1" runat="server" onclick="updateNotification()">Notifications</a>-->
<div id="notificationContainer">
<div id="notificationTitle">Notifications</div>
<div id="notificationsBody" class="Repeater">
<asp:Repeater ID="Repeater1" runat="server" EnableViewState="true" onitemcommand="view">
<ItemTemplate >
<p>
<tr>
<td><asp:Label ID="Label1" runat="server" CssClass="trHeader" Text='<%#Eval("NotificationTitle")%>'/></td>
</tr></p><p><br />
<tr class="trMessage">
<td>
<asp:LinkButton ID="nDescLbl" runat="server" CommandName="viewCase" Text='<%#Eval("NotificationDescription")%>' CssClass="trMessage"></asp:LinkButton>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("NotificationID")%>'></asp:Label>
<asp:HiddenField ID="nIdHf" runat="server" Value='<%#Eval("NotificationID")%>'/>
</td>
</tr></p><p><br />
<tr class="trFooter">
<td>
<asp:Label ID="Label3" runat="server" CssClass="trFooter" Text='<%#Eval("NotificationDate")%>'/></td>
</tr></p>
<br />
<hr style="color:darkgray;"/>
</ItemTemplate>
</asp:Repeater>
</div>
<div id="notificationFooter"><a href="Home.aspx" style="color:#006699;">See All</a></div>
</div>
[System.Web.Services.WebMethod]
public static string UpdateNotification(string nForId)
{
string returnValue = string.Empty;
try
{
using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
{
SqlCommand cmd = new SqlCommand("UpdateNotification", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nForId", nForId);
returnValue = cmd.ExecuteScalar().ToString();
connection.Close();
}
}
catch
{
returnValue = "error";
}
return returnValue;
}
这是不可能的,因为母版页是一种服务器端资源,IIS不会为其提供服务,它在您的
$.ajax
调用中被引用
如果您要求多个内容页能够访问WebMethod,请将代码移动到Web服务(ASMX)
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SiteService: System.Web.Services.WebService
{
[WebMethod]
public static string UpdateNotification(string nForId)
{
string returnValue = string.Empty;
try
{
using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
{
SqlCommand cmd = new SqlCommand("UpdateNotification", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nForId", nForId);
returnValue = cmd.ExecuteScalar().ToString();
connection.Close();
}
}
catch
{
returnValue = "error";
}
return returnValue;
}
}
如果#notificationLink
的唯一目的是调用javascript,那么您可以摆脱服务器控件
<a id="notificationLink">Notifications</a>
阅读更多信息:这是不可能的,因为母版页是IIS不会提供服务的服务器端资源,在您的
$.ajax
调用中引用了IIS
如果您要求多个内容页能够访问WebMethod,请将代码移动到Web服务(ASMX)
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class SiteService: System.Web.Services.WebService
{
[WebMethod]
public static string UpdateNotification(string nForId)
{
string returnValue = string.Empty;
try
{
using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
{
SqlCommand cmd = new SqlCommand("UpdateNotification", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nForId", nForId);
returnValue = cmd.ExecuteScalar().ToString();
connection.Close();
}
}
catch
{
returnValue = "error";
}
return returnValue;
}
}
如果#notificationLink
的唯一目的是调用javascript,那么您可以摆脱服务器控件
<a id="notificationLink">Notifications</a>
阅读更多信息:那么问题是什么?您遇到任何异常?您无法从母版页调用webmethod。如何在通知\u count\u span淡出后调用更新通知()。@MairajAhmad所以我必须在不同的类中创建web方法?以及如何调用更新通知功能?是的,您需要在aspx页面中编写webmethod。那么问题是什么?您遇到任何异常?您无法从母版页调用webmethod。如何在通知\u count\u span淡出后调用更新通知()。@MairajAhmad所以我必须在不同的类中创建web方法?以及如何调用更新通知函数?是的,您需要在aspx页面中编写webmethod。相反,我们可以使用WebAPI来定义服务器端方法。并且可以从客户端脚本调用此方法。这将更容易,但必须进行一些额外的代码更改,例如在Global.asax.cs文件中设置路由。Ref is我建议您使用浏览器控制台检查错误,并测试是否触发了单击事件..fadeToggle()和.fadeOut()正在执行。您应该看到ajax请求/响应,或者至少看到一个错误,表示找不到updateNotification()。我不知道还有什么好建议。很抱歉,我们可以使用Web API来定义服务器端方法。并且可以从客户端脚本调用此方法。这将更容易,但必须进行一些额外的代码更改,例如在Global.asax.cs文件中设置路由。Ref is我建议您使用浏览器控制台检查错误,并测试是否触发了单击事件..fadeToggle()和.fadeOut()正在执行。您应该看到ajax请求/响应,或者至少看到一个错误,表示找不到updateNotification()。我不知道还有什么好建议。很抱歉