0

我有这个按钮点击事件:

$('#btnDelete').on('click', function(userId)
{
    var treeView = $("#treeview").data("kendoTreeView");

    $('#treeview').find('input:checkbox:checked').each(function()
    {
        var li = $(this).closest(".k-item")[0];
        var notificationId = treeView.dataSource.getByUid(li.getAttribute('data-uid')).ID;
        $.ajax(
            {
                url: '../api/notifications/deleteNotification?userId=' + userId + '&notificationId=' + notificationId,
                type: 'DELETE',
                success: alert('Delete successful.'),
                failure: alert('Delete failed.')
            });
    });
});

“userId”来自基本页面:

public class BasePage : System.Web.UI.Page, IRequiresSessionState
{
    public int UserId
    {
        get
        {
            return T2Identity.UserId;
        }
    }
} 

如何在按钮“btnDelete”的单击事件中将 userId 变量传递给我的 JavaScript 函数?

4

3 回答 3

1

我能想到的一种方法是UserId在第一次发送页面时返回的 HtmlResponse 中将属性作为 cookie 传递。

另一种方法是在页面加载时将其传递到 Asp.NET 标记中。只需将其添加到页面的“正文”中:

<input type="hidden" id="user_id" name="user_id" value="<%= base.UserId.ToString() %>" />
于 2013-08-06T18:02:37.273 回答
0

可以传递给函数的唯一对象是事件(在您的情况下是单击)和触发事件的元素(在您的情况下是按钮)。

其他所有内容都必须作为全局变量传递,或附加到上述对象。例如,您可以将 userId 设置为按钮的属性。

于 2013-08-06T18:04:10.713 回答
0

您始终可以创建一个隐藏字段并将其传递给您的页面,然后读取该值。

或者如果你想从 webmethod 调用它

   [WebMethod]
    public string GetUserId()
    {
          return UserId.ToString();
    }

和 javascript

    <script type="text/javascript">
        function HandleIT() {
            PageMethods.GetUserId(); 
            function onSucess(result) {
                alert(result);
            }
            function onError(result) {
                alert('Something wrong.');
            }
        }
    </script>
于 2013-08-06T18:06:49.100 回答