how to bind javascript function with OnClientClick event with Eval?

南笙酒味 提交于 2020-01-01 02:53:10

问题


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.


回答1:


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



回答2:


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.




回答3:


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(&quot;alert(\&quot;{0}\&quot;); return false; &quot;, Eval(&quot;NAME&quot;))%>"**



回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/3829701/how-to-bind-javascript-function-with-onclientclick-event-with-eval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!