【发布时间】:2016-03-07 17:34:51
【问题描述】:
Jquery/Javascript: 想要在 notification_count_span 淡出并返回 false 后调用 updateNotification() 函数。 web 方法在母版页中。
<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>
HTML母版页中的 HTML 代码。
<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>
WebMethod 母版页中的 webmethod。
[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;
}
【问题讨论】:
-
那么问题是什么?你有什么例外吗?
-
您不能从母版页调用 webmethod。
-
notification_count_span 淡出后如何调用update notification()?
-
@MairajAhmad 所以我必须在不同的类中创建网络方法?以及如何调用更新通知功能?
-
是的,您需要在 aspx 页面中编写 webmethod。
标签: javascript jquery asp.net webmethod