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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3829701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:18:33  来源:igfitidea点击:

how to bind javascript function with OnClientClick event with Eval?

javascriptasp.netevalonclientclick

提问by ppp

my link button -

我的链接按钮 -

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

and the javascript msgDisp is-

和 javascript msgDisp 是-

<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.

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

回答by lincolnk

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

您可以将 的全部内容构建OnClientClick为代码括号内的字符串,它会像您期望的那样输出。

<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 OnClientClickserver side during the ItemDataBoundevent. Here's what it would like where the parent is a Repeatercontrol.

这是假设 LocationId 是一个有效数字 - 渲染时没有引号来包装您的值,因此输出类似的msgDisp(hello);内容会中断。我不知道如何以这种方式解决这个问题,所以如果你必须这样做,我建议OnClientClickItemDataBound活动期间设置服务器端。这是父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);
}

回答by bugventure

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 onclickevent.

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

回答by Edmund

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.

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

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

回答by Minhaj

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

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

回答by user2661454

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.

我要感谢林肯的回答。我目前正在帮助为 googam.com 建立一个新的社交网络。我一直在寻找解决方案来查看用户的个人资料,在数据列表中,在 jquery 模态对话框弹出窗口中。在ItemDataBound事件中设置linkbutton 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