相关文章推荐
帅呆的生菜  ·  python socket.error: ...·  1 年前    · 
深沉的书包  ·  Nodejs ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Is the link button in a databound control? If not, you need to call lbtnEdit.DataBind(). – Justin M. Keyes Aug 2, 2011 at 17:11

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);
                if you want quotes around it like in msgDisp('hello'); then you can use the HTML entity like: OnClientClick='<%# "msgDisp(&#39;" + Eval("LocationId") + "&#39;);" %>'.
– Shawn Kovac
                Mar 1, 2016 at 21:47

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.

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;))%>"**

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.