Dee*_*pak 2 javascript asp.net jquery webmethod
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
这是不可能的,因为 MasterPages 是服务器端资源,不会由 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;
}
}
Run Code Online (Sandbox Code Playgroud)
$.ajax({
type: "POST",
url: "SiteService.asmx/UpdateNotification",
...
Run Code Online (Sandbox Code Playgroud)
如果唯一的目的#notificationLink是调用 javascript,那么您可以摆脱服务器控制。
<a id="notificationLink">Notifications</a>
Run Code Online (Sandbox Code Playgroud)
$("#notificationLink").click(function (e) {
e.preventDefault();
$("#notificationContainer").fadeToggle(300);
$("#notification_count_span").fadeOut("slow");
updateNotification();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9012 次 |
| 最近记录: |