how to bind javascript function with OnClientClick event with 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 t开发者_StackOverflow社区he 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.
<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 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("alert(\"{0}\"); return false; ", Eval("NAME"))%>"**
You can do like OnClick='<%# "msgDisp(" + Eval("LocationId") + ");" %>'
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
精彩评论