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.
You can build the entire contents of
OnClientClick
as a string within the code brackets and it will output like you're expecting.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 settingOnClientClick
server side during theItemDataBound
event. Here's what it would like where the parent is aRepeater
control.You can do like OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'
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.
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.
//////////////
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.