最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call web method of master page? - Stack Overflow

programmeradmin6浏览0评论

Jquery/Javascript: Want to invoke updateNotification() Fuction after notification_count_span fade out and return false. web method is in master page.

<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 code in master page.

 <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" onitemmand="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 in master page.

[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;
}

Jquery/Javascript: Want to invoke updateNotification() Fuction after notification_count_span fade out and return false. web method is in master page.

<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 code in master page.

 <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" onitemmand="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 in master page.

[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;
}
Share Improve this question asked Dec 3, 2015 at 7:42 DeepakDeepak 971 gold badge2 silver badges8 bronze badges 7
  • So what is the issue? You are getting any exception? – Rahul Singh Commented Dec 3, 2015 at 7:49
  • You can't call webmethod from master page. – Mairaj Ahmad Commented Dec 3, 2015 at 7:52
  • how to call update notification() after notification_count_span fade out? – Deepak Commented Dec 3, 2015 at 7:52
  • @MairajAhmad so i have to create web method in different class? and how to call update notification function? – Deepak Commented Dec 3, 2015 at 7:53
  • Yes you need to write webmethod in aspx page. – Mairaj Ahmad Commented Dec 3, 2015 at 7:53
 |  Show 2 more ments

1 Answer 1

Reset to default 5

It is not possible due to MasterPages being a server side resource that will not be served by IIS, which is being referenced in your $.ajax call.

If you require that multiple content pages be able to access a WebMethod, move your code to a Web Service (ASMX) instead.

// To allow this Web Service to be called from script, using ASP.NET AJAX, unment 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;
    }
}
$.ajax({
    type: "POST",
    url: "SiteService.asmx/UpdateNotification",
    ...

If the only purpose of #notificationLink is to invoke javascript, then you can get rid of the server control.

<a id="notificationLink">Notifications</a>
$("#notificationLink").click(function (e) {
    e.preventDefault();
    $("#notificationContainer").fadeToggle(300);
    $("#notification_count_span").fadeOut("slow");
    updateNotification();
});

Read more: event.preventDefault()

发布评论

评论列表(0)

  1. 暂无评论