如何使用 Eval 将 javascript 函数与 OnClientClick 事件绑定?

发布于 2024-09-25 16:41:19 字数 497 浏览 3 评论 0原文

我的链接按钮 -

<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit" OnClientClick="javascript:msgDisp('<%# Eval(LocationId).toString() %>')" />

和 javascript msgDisp 是

<script type="text/javascript" language="javascript">
    function msgDisp(lid) {            
        alert(lid);
    }
</script>

- 但它没有在弹出窗口中给出 LocationId 而是整个字符串 <%#......%>正在弹出消息中。 如何在 javascript 中传递 Eval 值。

my link button -

<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit" OnClientClick="javascript:msgDisp('<%# Eval(LocationId).toString() %>')" />

and the javascript msgDisp is-

<script type="text/javascript" language="javascript">
    function msgDisp(lid) {            
        alert(lid);
    }
</script>

but it is not giiving LocationId in pop but the whole string <%#......%> is comin in popup message. How can I pass Eval values in javascript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

指尖上得阳光 2024-10-02 16:41:19

您可以将 OnClientClick 的全部内容构建为代码括号内的字符串,它将按照您的预期输出。

<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit" 
    OnClientClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>' /> 

假设 LocationId 是一个有效的数字 - 在呈现时没有引号来包装您的值,因此输出类似 msgDisp(hello); 的内容将会中断。我不知道如何以这种方式解决这个问题,因此如果您必须这样做,我建议在 ItemDataBound 事件期间设置 OnClientClick 服务器端。以下是父级为 Repeater 控件的情况。

protected void notesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    MyClass item = (MyClass)e.Item.DataItem;
    LinkButton lbtnEdit = (LinkButton)e.Item.FindControl("lbtnEdit");
    lbtnEdit.OnClientClick = string.Format("msgDisp('{0}');", item.LocationId);
}

You can build the entire contents of OnClientClick as a string within the code brackets and it will output like you're expecting.

<asp:LinkButton runat="server" ID="lbtnEdit" Text="edit" 
    OnClientClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>' /> 

This is assuming LocationId is a valid number- there are no quote marks to wrap your value when it renders, so outputting something like msgDisp(hello); is going to break. I don't know how to address that in this manner, so if you have to do that I would recommend setting OnClientClick server side during the ItemDataBound event. Here's what it would like where the parent is a Repeater control.

protected void notesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    MyClass item = (MyClass)e.Item.DataItem;
    LinkButton lbtnEdit = (LinkButton)e.Item.FindControl("lbtnEdit");
    lbtnEdit.OnClientClick = string.Format("msgDisp('{0}');", item.LocationId);
}
念﹏祤嫣 2024-10-02 16:41:19

如果您在标记中呈现了绑定表达式标签 (<%# ... %>),则意味着您的 LinkBut​​ton 未在绑定容器中初始化。正如 @lincolnk 所演示的,绑定容器可以是 Repeater 或 GridView 项、日历单元格等。此外,您不必在函数调用前添加“javascript:”前缀。 OnClientClick 属性的值呈现为锚点的 onclick 事件的处理程序。

If you are getting your binding expression tags (<%# ... %>) rendered in the markup, it means your LinkButton is not initialized in a binding container. A binding container can be, as @lincolnk demonstrated, an Repeater or GridView item, a Calendar cell, etc. Also, you do not have to prefix your function call with "javascript:". The value of the OnClientClick property is rendered as the handler of the anchor's onclick event.

GRAY°灰色天空 2024-10-02 16:41:19

网上到处找。每个人都说使用CodeBehind。
请参阅我的解决方案,即使我的数据值中像奥尼尔一样包含单引号,该解决方案也能正常工作。
如果您的数据项包含双引号,这将不起作用。但适用于我需要它做的事情,即以一个人的名义传递。
请注意警报调用中的反斜杠。

OnClientClick="<%#string.Format("alert(\"{0}\"); return false; ", Eval("NAME"))%>"**

Looked everywhere on the net. Everyone says use CodeBehind.
See my solution, which works even when my datavalue has a single quote in it like O'Neal.
This will not work if your data item contains doublequotes. But works for what I needed it to do which was pass in a person's name.
Note the backslashes inside the alert call.

OnClientClick="<%#string.Format("alert(\"{0}\"); return false; ", Eval("NAME"))%>"**
も让我眼熟你 2024-10-02 16:41:19

你可以这样做
OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'

You can do like
OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'

追星践月 2024-10-02 16:41:19

我要感谢林肯的回答。我目前正在帮助 googam.com 建立一个新的社交网络。我几天来一直在寻找一种解决方案,可以在数据列表中、jquery 模式对话框弹出窗口中查看用户的个人资料。在 ItemDataBound 事件中设置链接按钮 OnClientClick 解决了将用户 id 传递给 JQuery 函数以在弹出窗口中打开 acsx 用户控件的问题。

    jQuery(document).ready(function () {
        var mydiv = jQuery("#mydialog").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            width: '500',
            height: '400'
        }).css("font-size", "0.8em");
    });

    function ShowPopup(uid) {
        var mydiv = jQuery("#mydialog")
        //alert(uid)
        // Load the content using AJAX
        mydiv.load('Profile.aspx?id=' + uid);
        // Open the dialog        
        mydiv.dialog('open');
    }

///////////////////////////

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim imageControl = TryCast(e.Item.FindControl("Image1"), Image)
        Dim Uid As String = imageControl.ImageUrl

        Dim ProfileBtn As LinkButton = TryCast(e.Item.FindControl("ProfileButton"), LinkButton)
        ProfileBtn.OnClientClick = String.Format("ShowPopup('{0}');return false;", Uid)

    End If
End Sub

I want to thank lincolnk for his answer. I'm currently helping to build a new social network for googam.com. I have been searching for a few days for a solution to view a user's profile, in a datalist, in a jquery modal dialog popup. Setting the linkbutton OnClientClick in the ItemDataBound event solved the problem of passing the user id to the JQuery function to open a acsx user control in the popup window.

    jQuery(document).ready(function () {
        var mydiv = jQuery("#mydialog").dialog({
            autoOpen: false,
            resizable: false,
            modal: true,
            width: '500',
            height: '400'
        }).css("font-size", "0.8em");
    });

    function ShowPopup(uid) {
        var mydiv = jQuery("#mydialog")
        //alert(uid)
        // Load the content using AJAX
        mydiv.load('Profile.aspx?id=' + uid);
        // Open the dialog        
        mydiv.dialog('open');
    }

//////////////

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim imageControl = TryCast(e.Item.FindControl("Image1"), Image)
        Dim Uid As String = imageControl.ImageUrl

        Dim ProfileBtn As LinkButton = TryCast(e.Item.FindControl("ProfileButton"), LinkButton)
        ProfileBtn.OnClientClick = String.Format("ShowPopup('{0}');return false;", Uid)

    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文